E
E
Evgeny Milushkin2015-05-25 09:13:44
Yii
Evgeny Milushkin, 2015-05-25 09:13:44

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

2 answer(s)
A
Alexander Makarov, 2015-05-25
@evgenmil

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.');
        }
}

B
bxN5, 2016-06-04
@bxN5

here is a good article on rules and scripts phpinfo.info/yii2-scenario-rules

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question