How to validate email and email already exist or not check, in Yii Framework?

后端 未结 6 1021
日久生厌
日久生厌 2020-12-18 21:15

How to validate email using Yii Model validation rules function code. Also how to check email exist or not using Model validation rules function in Yii.

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-18 21:37

    You can create your custom validation method to fulfill your requirement.

    Create a function in model class:

    public function uniqueEmail($attribute, $params)
    {
         // Set $emailExist variable true or false by using your custom query on checking in database table if email exist or not.
        // You can user $this->{$attribute} to get attribute value.
    
         $emailExist = true;
    
         if($emailExist)
        $this->addError('email','Email already exists');
    }
    

    User this validation method in rules:

    array('email', 'uniqueEmail','message'=>'Email already exists!'),    
    

提交回复
热议问题