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.

Disable validator in model for single update

Hello everyone I need a help with how validator works in phalcon model.

I have user model that contains password field and has length validation rule for the password. When I am inserting new rules and validation doesnt fail, the password is hashed in model method afterValidationOnCreate() and the hash is then inserted into db.

Now when I want to update the user row, validator always complains at the password length since the hash is longer than my max allowed length for password.

My idea was doing something like $user = User::findFirst($id); $user->setName('newusername'); $user->update();

But even though I am not updating the password field, it is still being validated and fails the update. Also phql doesnt work either as it requires models which produces same issue.

So is there a way to disable validator for single query or stop it from validating fields that havent changed? I have tried putting " $this->useDynamicUpdate(true);" into initialize method but that didnt help.

Thanks!



1.4k
Accepted
answer

Fixed the issue by making a variable $_runValidator = true; inside the model and adding method disableValidator() to set the state and validatorEnabled() to get the state. Then on top of validation() method just if ! validatorEnabled() return true That efectively stops the validation for model on demand and thus solves my issue.

If anyone have anything to add to this, please do so as I might consider changing the functionality later.

I ran into the same issue, and that's the only solution I could think of. Unless, you send the password too on every update!



13.5k

This should be added to the frmework.