A
A
akula222019-07-20 21:27:01
Yii
akula22, 2019-07-20 21:27:01

How to write a validator to determine the phone number, username or email. the address?

in the login model created the property $login displayed it in the form as "Phone number, username or e-mail address"
Help create a validator that will determine whether the user entered, phone, name, or email

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2019-07-20
@sidni

in short then

public function rules()
    {
        return [
            ['login', 'validateLogin'],
        ];
    }

    public function validateLogin($attribute, $params, $validator)
    {
        if (!is_telephone($this->$attribute) && !is_email($this->$attribute) && !is_name($this->$attribute)) {
            $this->addError($attribute, 'Должен быть емейлом, телефоном или именем');
        }
    }

PS
is_telephone, is_email, is_name
Your custom functions (maybe methods) for validation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question