G
G
Greg Popov2016-08-14 22:11:01
Yii
Greg Popov, 2016-08-14 22:11:01

How in Yii2 to specify models at login a choice from two fields?

Hello. How to login using either username or email in Yii2?
So far, the idea is only with "range"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2016-08-15
@slo_nik

Try in LoginForm, in getUser search either by login or by email.
Set the validation rules accordingly so that you don’t swear at the absence of one of the parameters, for the login, if instead of the login email and for email, if the login is specified instead of email.

A
Andrew, 2016-08-15
@mhthnz

In the findByUsername method, add a search by the email field.

public static function findByUsername($username)
    {
        return static::find()->where(['username' => $username])->orWhere(['email' => $username])->one();
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question