Как вывести ошибку валидации для old_password формы в Yii2?
$this->addError() не работает.
public static function tableName()
{
return 'user';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['first_name', 'last_name', 'email', 'repeat_password', 'old_password', 'new_password'], 'required'],
[['is_admin'], 'integer'],
[['created'], 'safe'],
[['first_name', 'last_name', 'email', 'auth_key', 'token'], 'string', 'max' => 191],
[['repeat_password', 'old_password', 'new_password'], 'string', 'max' => 191],
['old_password', 'validatePassword'],
['repeat_password', 'compare', 'compareAttribute' => 'new_password'],
];
}
/**
* @inheritdoc
*/
public function validatePassword()
{
if (!$this->hasErrors()) {
$u = User::find()->where(['id' => Yii::$app->user->id])->one();
$user = $u->password;
if (Yii::$app->getSecurity()->validatePassword($this->old_password, $user)) {
var_dump('success');
}else{
$this->addError('old_password', 'Incorrect username or password.');
// var_dump('error');
}
}
}
и controller
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->password = \Yii::$app->getSecurity()->generatePasswordHash($model->repeat_password);
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}