We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

How to skip a validation if the form is empty?

Hi there, i am creating a validator for a number input. But, if the input is empty, it will still return true. How can i do it ?

The current code is like this :

$rank = new Text("rank");

$rank->setLabel("Rank");

$rank->addValidators(array(

new Numericality(array('message' => 'Rank must be a number'))

));

Thanks in advance :)



6.4k
Accepted
answer

Use allowEmpty option

$rank->addValidators(array(
    new Numericality(array('message' => 'Rank must be a number', 'allowEmpty' => TRUE))
));

notice that 0 is an empty value



6.6k

...I should have known that. Thanks :D