Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Suggestion for validator

I use email validator in my code, but when the value is empty string, it still validate, and throw the message. I think there is PresenceOf validator can do this job, if the value must be filled, then use PresenceOf. Now my email field can be empty, but I still want to validator it when it has value, how can I do? Can phalcon fix this?



4.4k

Simply add an array of validators.

Check this: https://github.com/phalcon/vokuro/blob/master/app/forms/SignUpForm.php#L38

$email->addValidators(array(
    new PresenceOf(array(
        'message' => 'The e-mail is required'
    )),
    new Email(array(
        'message' => 'The e-mail is not valid'
    ))
));


3.8k
Accepted
answer

thanks!but that‘s not my question. I found it in the phalcon's source file

new Email([ 'message' => 'The e-mail is not valid', 'allowEmpty' => true ])

just set the 'allowEmpty' to true. phalcon's doc haven't write this.

Simply add an array of validators.

Check this: https://github.com/phalcon/vokuro/blob/master/app/forms/SignUpForm.php#L38

```php $email->addValidators(array( new PresenceOf(array( 'message' => 'The e-mail is required' )), new Email(array( 'message' => 'The e-mail is not valid' )) )); ```