I cannot figure out how to edit an on-the-fly validation rule, for example, in my controller.
My case: the "users" table has an "email" field, the value of which must be "unique" when creating and updating. This is normal at the moment, I created the correct validation rules.
But now I need to create an action that allows users to reset their password. Thus, there is a form in which users enter their email address, and this form needs to be verified. After that, the action checks for the presence of this email address and sends an email to the reset address.
So: I have to validate the form using validation rules, but in this particular case I don’t need the email to be “unique”.
How to change a validation rule for only one action?
Thanks.
EDIT
Maybe this?
class UsersTable extends Table { public function validationDefault(\Cake\Validation\Validator $validator) { //Some rules... $validator->add('email', [ 'unique' => [ 'message' => 'This value is already used', 'provider' => 'table', 'rule' => 'validateUnique' ] ]); //Some rules... return $validator; } public function validationOnlyCheck(\Cake\Validation\Validator $validator) { $validator->remove('email', 'unique'); return $validator; } }
In my action:
$user = $this->Users->newEntity($this->request->data(), [ 'validate' => 'OnlyCheck' ]);
source share