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

Ignore other validators if one fails

Hey guys,

I'm sure I've seen this somewhere before, but I just can't find the topic again.

Let's say I create form like in here: https://docs.phalcon.io/en/latest/reference/forms.html#validation and add 2 validators to my name field. Currently, when field is empty they both seem to fire and I end up with stupid error messages 'Name is required' 'Name must be longer than 6' and so on.

Is is possible to ignore remaining form validators if one of them fails? So only one message is generated per field?

Thanks

edit Just found this: https://github.com/phalcon/cphalcon/issues/1540 so I guess it's not possible at the moment.



2.0k

there is option to stop the validation of validator fails, read here: https://docs.phalcon.io/en/latest/reference/validation.html#cancelling-validations but I didnt get around to actualy do anything with that so dont know if by "chain" in documentation they mean all validators or just the validators with same id

You could test it :)

Also you could make your own validator for your field so you could just put some conditions in your validator to do stuff you need and eventualy stop if problem is found. Which would then jump on next validator for next field.



15.1k

Ha, I knew I've seen it somewhere. That's exactly it, thanks!