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.

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.3k
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



2.7k

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