P
P
Pavel Gogolinsky2014-09-18 08:38:23
Yii
Pavel Gogolinsky, 2014-09-18 08:38:23

How to make require validation with OR in yii?

How to use require validator with OR in Yii model.
For example, it requires a person to enter a phone number or email address, but one or both is required.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Max, 2014-09-18
@gogolinsky

public function rules() {
    return array(
        array('username','either','other'=>'email'),
    );
}
public function either($attribute_name, $params)
{
    $field1 = $this->getAttributeLabel($attribute_name);
    $field2 = $this->getAttributeLabel($params['other']);
    if (empty($this->$attribute_name) && empty($this->$params['other'])) {
        $this->addError($attribute_name, Yii::t('user', "either {$field1} or {$field2} is required."));
        return false;
    }
    return true;
}

A
Alexander Zelenin, 2014-09-18
@zelenin

https://github.com/yiisoft/yii2/blob/master/docs/g...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question