Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make email unique in yii user model

Tags:

yii

validation code is

 return array( 
         array('firstname, lastname, confirm_email, education, email, password, occupation,location , birthdate, interest,gender,created, modified', 'required'),

                     array('email', 'email'),
                     array('password', 'length', 'max'=>20, 'min' => 5,'message' => "Incorrect fi (length between 5 and 20 characters)."),
                     array('firstname', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => UserModule::t("Incorrect symbols (A-z0-9).")),
                     array('email', 'unique'),
    );
like image 603
Er Parveen Saini Avatar asked Sep 07 '12 11:09

Er Parveen Saini


2 Answers

You may make your email unique in yii user model by the following rule as given in below.

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

Here className is the name of your user model class, attributeName is your db email field name.

You may also check the below link.

http://www.yiiframework.com/forum/index.php/topic/32786-creating-my-own-model-cmodel-not-cactiverecord/

Thanks

like image 182
Muhammad Rizwan Kaim Khani Avatar answered Sep 19 '22 14:09

Muhammad Rizwan Kaim Khani


public function rules()
{
    return array(
        ...
        array('email', 'email'),
        array('email', 'unique'),
        ...
    );
}
like image 45
ahmed Avatar answered Sep 18 '22 14:09

ahmed