I've setup a model and behavior where the behavior contains some custom validation rule methods like match for ensuring two fields have identical values, and this works, but there are some very generic $validate rules that I'd like to reuse in different models for things like passwords. When I put the $validate array into my ValidateBehavior and invoke validate in my controller, it doesn't appear to hit any validations and everything passes even though the fields are incorrect.
Can I use $validate in my behavior have my model use it and work when I invoke validate on a post? This post seems to suggest I can, but it isn't working.
MODEL: class Admin extends AppModel { public $name = 'Admin'; public $actsAs = [ 'Validate' ]; } BEHAVIOR: class ValidateBehavior extends ModelBehavior { public $validate = [ 'currentpassword' => [ 'notEmpty' => [ 'rule' => 'notEmpty', 'message' => 'Current password is required.' ], 'minLength' => [ 'rule' => [ 'minLength', '8' ], 'message' => 'Passwords must be at least 8 characters long.' ] ], 'newpassword' => [ 'notEmpty' => [ 'rule' => 'notEmpty', 'message' => 'New password is required.' ], 'minLength' => [ 'rule' => [ 'minLength', '8' ], 'message' => 'Passwords must be at least 8 characters long.' ], 'match' => [ 'rule' => [ 'match', 'confirmpassword' ], 'message' => 'New password must match the confirmation password' ] ], ... etc public function match( Model $Model, $check, $compareTo ) { $check = array_values( $check )[ 0 ]; $compareTo = $this->data[ $this->name ][ $compareTo ]; return $check == $compareTo; } } function changepassword() { $post = $this->request->data; // Was there a request to change the user's password? if ($this->request->is( 'post' ) && !empty( $post )) { // Set and validate the post request $this->Admin->set( $this->request->data ); // Set of validation rules to be run $validateRules = [ 'fieldList' => [ 'currentpassword', 'newpassword', 'confirmpassword' ] ]; if ($this->Admin->validates( $validateRules )) { // makes it here even though all fields are empty when // the validation rules are in the behavior otherwise // when left in the model and the behavior only has // the methods like match this all works ...etc } ...etc }