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

Validate Form but allow empty values

Using Forms and Validator as well, I want to validate the field but skipping the empty value, its an optional field, but if the value of the field is empty the validator return anyway.

$phoneMobile = new Text('mobile');
        $phoneMobile->setFilters([
            'striptags', 'string'
        ]);
        $phoneMobile->addValidator(
            new Regex([
                'pattern' => '/^[0-9]+$/',
                'message' => 'Provide only numbers!!'
            ])
        );
        $this->add($phoneMobile);
edited Oct '15

in Phalcon 2.0 you must add ''allowEmpty" => true to your validator

$phoneMobile->addValidator(
        new Regex([
            'pattern' => '/^[0-9]+$/',
            'message' => 'Provide only numbers!!',
            'allowEmpty' => true
        ])
    );

in Phalcon 2.1.0 Beta 2 is enabled by default

Added internal check "allowEmpty" before calling a validator. If it option is true and the value of empty, the validator is skipped
edited Oct '15

OK I understand, but how can I check the empty value directily on my Form Class??

$phoneMobile->addValidator(
            new Regex([
                'pattern' => '/^[0-9]+$/',
                'message' => 'Provide only numbers!!'
            ])
        );

I updated my answer. This should work.