Answer the question
In order to leave comments, you need to log in
Why doesn't native validation work in yii2?
gentlemen, there is a problem: I do my own validation, add to the rules:
['role', 'required'],
['role', 'validateRole'],
// Role validation:
public function validateRole($attribute, $params) {
if (!in_array($this->$attribute, ['admin','distributor','reseller','customer'])) {
$this->addError($attribute, 'Wrong Role');
}
}
// Role validation:
public function validateRole($attribute, $params) {
$this->addError($attribute, 'Wrong Role');
}
Exception 'ReflectionException' with message 'Class validateRole does not exist'
Answer the question
In order to leave comments, you need to log in
why reinvent the wheel?
['role', 'required'],
['role', 'in', 'range' => ['admin', 'distributor', 'reseller', 'customer'], 'message' => 'Wrong Rule'],
Press submit and you will get an error. And on the client you need to do clientValidation
Sergey : yes, below you were answered correctly. It shouldn't have worked on the frontend, sorry, I just didn't notice. For the frontend, you need to write your own rules, see
www.yiiframework.com/doc-2.0/yii-validators-valida...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question