title: How to authenticate only email during login in yii2?
body: <p>I want to login the user with their email id but it gives me the error when the email is not exists in the database, if the email exists it becomes login with no error. I am also using validateEmail function but it gives error. which is</p>
  
  <pre><code>PHP Recoverable Error – yii\base\ErrorException
  
  Argument 1 passed to yii\web\User::login() must implement interface yii\web\IdentityInterface, null given, called in C:\xampp\htdocs\newone\common\models\LoginForm.php on line 68 and defined
  </code></pre>
  
  <hr>
  
  <p>in controller the actionLogin is as follows</p>
  
  <pre><code>public function actionLogin()
      {
         // $this-&gt;layout = 'login_layout';
          if (!\Yii::$app-&gt;user-&gt;isGuest) {
              return $this-&gt;goHome();
          }
  
          $model = new LoginForm();
  
  
          if ($model-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $model-&gt;login()) {
  
              return $this-&gt;goBack();
          } else {
              return $this-&gt;render('login', [
                  'model' =&gt; $model,
              ]);
          }
  </code></pre>
  
  <h2>        }</h2>
  
  <p>the User model is as follows</p>
  
  <pre><code>&lt;?php
  namespace common\models;
  
  use Yii;
  use yii\base\NotSupportedException;
  use yii\behaviors\TimestampBehavior;
  use yii\db\ActiveRecord;
  use yii\web\IdentityInterface;
  
  /**
   * User model
   *
   * @property integer $id
    * @property integer $speaker_id
   * @property string $username
   * @property string $password_hash
   * @property string $password_reset_token
   * @property string $email
   * @property string $auth_key
   * @property integer $status
   * @property string $type
   * @property integer $created_at
   * @property integer $updated_at
   * @property string $password write-only password
   */
  class User extends ActiveRecord implements IdentityInterface
  {
  
      const STATUS_DELETED = 0;
      const STATUS_ACTIVE = 10;
  
  
      /**
       * @inheritdoc
       */
      public static function tableName()
      {
          return '{{%user}}';
      }
  
      /**
       * @inheritdoc
       */
      public function behaviors()
      {
          return [
              TimestampBehavior::className(),
          ];
      }
  
          /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
              [['speaker_id', 'phone_number', 'status'], 'integer'],
              [['fullname', 'country', 'user_image', 'image_path', 'auth_key', 'password_hash', 'email', 'type'], 'required'],
              [['created_at', 'updated_at'], 'safe'],
              [['fullname', 'password_hash', 'password_reset_token', 'email'], 'string', 'max' =&gt; 255],
              [['country'], 'string', 'max' =&gt; 500],
              [['user_image', 'image_path'], 'string', 'max' =&gt; 300],
              [['auth_key'], 'string', 'max' =&gt; 32],
              [['type'], 'string', 'max' =&gt; 10],
              [['email'], 'unique'],
          ];
      }
  
  
  
      /**
       * @inheritdoc
       */
      public function attributeLabels()
      {
          return [
              'id' =&gt; 'ID',
              'speaker_id' =&gt; 'Speaker ID',
              'fullname' =&gt; 'Fullname',
              'phone_number' =&gt; 'Phone Number',
              'country' =&gt; 'Country',
              'user_image' =&gt; 'User Image',
              'image_path' =&gt; 'Image Path',
              'auth_key' =&gt; 'Auth Key',
              'password_hash' =&gt; 'Password Hash',
              'password_reset_token' =&gt; 'Password Reset Token',
              'email' =&gt; 'Email',
              'status' =&gt; 'Status',
              'type' =&gt; 'Type',
              'created_at' =&gt; 'Created At',
              'updated_at' =&gt; 'Updated At',
          ];
      }
  
  
  
      /**
       * @inheritdoc
       */
      public static function findIdentity($id)
      {
          return static::findOne(['id' =&gt; $id, 'status' =&gt; self::STATUS_ACTIVE]);
      }
  
      /**
       * @inheritdoc
       */
      public static function findIdentityByAccessToken($token, $type = null)
      {
          throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
      }
  
      /**
       * Finds user by username
       *
       * @param string $username
       * @return static|null
       */
      public static function findByEmail($email)
      {
          return static::findOne(['email' =&gt; $email, 'status' =&gt; self::STATUS_ACTIVE]);
      }
  
      /**
       * Finds user by password reset token
       *
       * @param string $token password reset token
       * @return static|null
       */
      public static function findByPasswordResetToken($token)
      {
          if (!static::isPasswordResetTokenValid($token)) {
              return null;
          }
  
          return static::findOne([
              'password_reset_token' =&gt; $token,
              'status' =&gt; self::STATUS_ACTIVE,
          ]);
      }
  
      /**
       * Finds out if password reset token is valid
       *
       * @param string $token password reset token
       * @return bool
       */
      public static function isPasswordResetTokenValid($token)
      {
          if (empty($token)) {
              return false;
          }
  
          $timestamp = (int) substr($token, strrpos($token, '_') + 1);
          $expire = Yii::$app-&gt;params['user.passwordResetTokenExpire'];
          return $timestamp + $expire &gt;= time();
      }
  
      /**
       * @inheritdoc
       */
      public function getId()
      {
          return $this-&gt;getPrimaryKey();
      }
  
      /**
       * @inheritdoc
       */
      public function getAuthKey()
      {
          return $this-&gt;auth_key;
      }
  
      /**
       * @inheritdoc
       */
      public function validateAuthKey($authKey)
      {
          return $this-&gt;getAuthKey() === $authKey;
      }
  
      /**
       * Validates password
       *
       * @param string $password password to validate
       * @return bool if password provided is valid for current user
       */
      public function validatePassword($password)
      {
          return Yii::$app-&gt;security-&gt;validatePassword($password, $this-&gt;password_hash);
      }
  
      /**
       * Generates password hash from password and sets it to the model
       *
       * @param string $password
       */
      public function setPassword($password)
      {
          $this-&gt;password_hash = Yii::$app-&gt;security-&gt;generatePasswordHash($password);
      }
  
      /**
       * Generates "remember me" authentication key
       */
      public function generateAuthKey()
      {
          $this-&gt;auth_key = Yii::$app-&gt;security-&gt;generateRandomString();
      }
  
      /**
       * Generates new password reset token
       */
      public function generatePasswordResetToken()
      {
          $this-&gt;password_reset_token = Yii::$app-&gt;security-&gt;generateRandomString() . '_' . time();
      }
  
      /**
       * Removes password reset token
       */
      public function removePasswordResetToken()
      {
          $this-&gt;password_reset_token = null;
      }
  
  
      public function getProfile()
      {
          return $this-&gt;hasOne(UserProfile::className(), ['user_id' =&gt; 'id']);
      }
  
  
  }
  </code></pre>
  
  <hr>
  
  <p>the LoginForm model is as follows </p>
  
  <pre><code>&lt;?php
  namespace common\models;
  
  use Yii;
  use yii\base\Model;
  
  /**
   * Login form
   */
  class LoginForm extends Model
  {
      public $email;
      public $password;
      public $rememberMe = true;
  
      private $_user;
  
  
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
              // username and password are both required
              [['email'], 'required'],
              // rememberMe must be a boolean value
              ['rememberMe', 'boolean'],
              // password is validated by validateEmail()
              ['email', 'email'],
          ];
      }
  
      /**
       * Validates the password.
       * This method serves as the inline validation for password.
       *
       * @param string $attribute the attribute currently being validated
       * @param array $params the additional name-value pairs given in the rule
       */
      public function setEmail($attribute, $params)
      {
          if (!$this-&gt;hasErrors()) {
              $user = $this-&gt;getUser();
              if (!$user || !$user-&gt;validateEmail($this-&gt;email)) {
                  $this-&gt;addError($attribute, 'Incorrect email or not found.');
              }
          }
      }
  
      // public function validateEmail($attribute, $params)
      // {
      //     // if($user = User::find()-&gt;exists('email=:email',array('email'=&gt;$this-&gt;email)))
      //     if($user = User::find()-&gt;where('email=:email',['email' =&gt; $this-&gt;email])-&gt;exists())
  
      //       $this-&gt;addError($attribute, 'Email already exists!');
  
      // }
  
      /**
       * Logs in a user using the provided username and password.
       *
       * @return bool whether the user is logged in successfully
       */
      public function login()
      {
          if ($this-&gt;validate()) {
              return Yii::$app-&gt;user-&gt;login($this-&gt;getUser(), $this-&gt;rememberMe ? 3600 * 24 * 30 : 0);
          } else {
              return false;
          }
      }
  
      /**
       * Finds user by [[username]]
       *
       * @return User|null
       */
      protected function getUser()
      {
          if ($this-&gt;_user === null) {
              $this-&gt;_user = User::findByEmail($this-&gt;email);
          }
  
          return $this-&gt;_user;
      }
  }
  </code></pre>