Я установил модель и поведение, когда поведение содержит некоторые настраиваемые методы проверки правильности, такие как match для обеспечения того, что два поля имеют одинаковые значения, и это работает, но есть некоторые очень общие $ подтвердите правила, которые я хотел бы использовать в разных моделях для таких вещей, как пароли. Когда я помещаю массив $ validate в мое ValidateBehavior и вызывается проверка в моем контроллере, он, похоже, не попадает ни в какие проверки, и все проходит, даже если поля неверны.
Могу ли я использовать $ validate в своем поведении, если моя модель использует его и работает, когда я вызываю проверку на сообщение? Это post кажется, что я могу, но он не работает.
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
}