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

How to use custom validator? It wont stop if fails!

Inside a \Phalcon\Forms\Form I add my own valdator:

$item->addValidator(new \CustomEmailValidator(array(
  'message' => 'bad!')
));

this looks like:

class CustomEmailValidator extends Phalcon\Validation\Validator implements Phalcon\Validation\ValidatorInterface

{

public function validate(\Phalcon\Validation $validator, $attribute)

{

return false;

}

}

the problem is, no matter what I return, it wont stop the validator. I expect it failed, just like I had set any other validator



85.5k
Accepted
answer
edited Oct '15

my message is a bit spammy, but i remeber when i was trying same stuff I couldnt make it work, then I moved to 2.1.x where validations seems to be working.

if it is any help there are some additinal validators ( i think ) in incubator github repo. Check their code and try it yourself to see if any changes apear.



28.2k

something like that. All I had to do this before returning false, adding the message:

$validator->appendMessage(new \Phalcon\Validation\Message('message', $attribute, 'CustomEmailValidator'));