Answer the question
In order to leave comments, you need to log in
[[+content_image]]
Yii2. How to check if one of the two required fields is filled?
Validation rule:
...
[['field1', 'field2'], 'validateFields', 'skipOnEmpty' => false, 'skipOnError' => false],
...
public function validateFields($attribute, $params)
{
// делаю очистку ошибок обоих полей (это не работает)
$this->clearErrors('field1');
$this->clearErrors('field2');
// проверяю
if (empty($this->field1) && empty($this->field2)) {
// в первый input помещаю пустую ошибку (поля идут вертикальным списком)
$this->addError('field1', '');
// во второй инпут помещаю саму ошибку
$this->addError('field2', 'At least one must not be blank.');
return false;
}
return true;
}
...
'enableAjaxValidation' => true,
'enableClientValidation' => false,
...
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question