We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

check if variable is empty using \PresenceOf

Hello, I'm new to PHP Phalcon. I have a variable called username and I'm developing an Arabic website. so when I check if varaible is empty I use this approach

$this->validate(new PresenceOf(array( 'field' => 'username', 'message' => 'You need to type username' )));

it works when I type English characters but if I type Arabic characters it return error message 'you need to type username'.

how to solve this problem? thanks :)



26.3k
Accepted
answer
edited Sep '14

Are you sure it is exactly "you need to type username"?

Try to include this in your model class in your initialize() method:


class YourModel extends YourOrPhalconBaseModel {

  public function initialize() {

    $this->setup(array(
        'notNullValidations'=>false,
    ));

  }

}

Be carefull because this switch off Phalcon's authomatic PresenceOf validators in this model. It means that if:

  • you do not set up you own PresenceOf validators for all fields
  • and you will try to save an entity to DB and one field will be empty

Then exception will be thrown.

I found my mistake, I'm learning using the examples that provided in documentation and I have copied UserController from one of them.

the problem was in this code

$username = $request->getPost('username', 'alphanum');

so I just removed the second parameter and now it works well :)

Thank you and I'm sorry for my very simple mistake.