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

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!



2.0k
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!



22.1k

This should be added to the frmework.