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

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?



6.6k

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'
    ))
));


6.7k
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

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