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.

ORM validation before my own validations

I'm trying to validate a a field name "my_field" using the classic public function validation() in my model. The ORM seems to validate it before. I only get "my_field is required". My function validation() is not even triggered.

Is there a way to make my validation fire before the one from the ORM?



1.7k

You could use the beforeValidation method. It is executed before the database validations are made.

Although intended I find it strange the user validation is run after the database validations/foreignkey checks.



17.9k
Accepted
answer

I'm trying to validate a a field name "my_field" using the classic public function validation() in my model. The ORM seems to validate it before. I only get "my_field is required". My function validation() is not even triggered. Is there a way to make my validation fire before the one from the ORM?

I switch off the default notNullValidations in the model. Do this in initialisation() method of the model. Check these post/docs:

(1) http://forum.phalconphp.com/discussion/2547/give-custom-model-validation-priority

(2) http://forum.phalconphp.com/discussion/1497/validation-with-presenceof-not-working

(3) http://forum.phalconphp.com/discussion/1497/validation-with-presenceof-not-working

(4) http://docs.phalconphp.com/en/latest/reference/models.html#disabling-enabling-features

This way Phalcon will not generate these user-unfriendly my_field is required messages. Instead of them I am adding PresenceOf validators to validation() method.

Both solutions seems to work. Thanks guys.



11.9k

@Conradaek, thank you for answer! Save a lot of my time – just faced that Model::validation() doesn't works and ORM sends data directly to database through my validations.