Answer the question
In order to leave comments, you need to log in
How to create a validation rule in Yii2 with an "OR" condition?
When filling out a form, how to specify a validation rule for one OR another field to be filled in? For example, email or phone numbers.
Answer the question
In order to leave comments, you need to log in
public function rules()
{
return [
[['email', 'phone'], 'atLeastOneRequired'],
];
}
public function atLeastOneRequired($attribute, $params)
{
if (empty($this->email) && empty($this->phone)) {
$this->addError($attribute, 'Please enter either email or phone.');
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question