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

32,596

Solution 1

You can set your model validations as below

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
            //First parameter is your field name of table which has email value
        array('email', 'email','message'=>"The email isn't correct"),
        array('email', 'unique','message'=>'Email already exists!'),            
    );
}

Yii Reference Link For More Detail: http://www.yiiframework.com/wiki/56/

Solution 2

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!'),    

Solution 3

For Yii2 I used the following in a model called Register which will use the User Class.

public function rules()
{
    return [

        ['Email', 'filter', 'filter' => 'trim'],
        ['Email', 'required'],
        ['Email', 'email'],
        ['Email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

    ];
}

You need to use targetClass and put the Namepsace for the Class User

Solution 4

Custom validation, short and sweet code. try this it's working fine -

public function rules(){   
        return array(
            array('email, first_name, last_name, password, repeat_password', 'required'),
            array('email', 'email','message'=>"The email isn't correct"),
            array('email', 'uniqueEmail'),
        );  
    }

write this custom function in the same model -

public function uniqueEmail($attribute, $params){
        if($user = User::model()->exists('email=:email',array('email'=>$this->email)))
          $this->addError($attribute, 'Email already exists!');
    }

Solution 5

You can easily find either email is already present in your db or not by defining the rule.

Here is the rule.

array('xxx', 'unique', 'className' => 'SomeClass', 'attributeName' => 'SomeAttribute'),

Example.

public function rules() {
   return array(
     ...
     array('email', 'unique', 'className' => 'User', 'attributeName' => 'email', 'message'=>'This Email is already in use'),
     ...
); 
}

Here i want to put validation on Email, which is unique, my model class name is User, attributeName is the field name of the table i.e. email and if email is already present in your table then display message.

If it gives error then you may change your table and make unique the email field.

ALTER TABLE user ADD UNIQUE (email)

Then check.

other email validations are in below. which i think complete set of email validation.

public function rules() {
   return array(
     ...
     array('email', 'required'), 
     array('email', 'length', 'max'=>200),
     array('email', 'email', 'message'=>'Email is not valid'),
     array('email', 'unique', 'className' => 'User', 'attributeName' => 'email', 'message'=>'This Email is already in use'),
     ...
); }

That's it. Thanks

Share:
32,596
Admin
Author by

Admin

Updated on August 28, 2020

Comments

  • Admin
    Admin over 3 years

    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.