Er Parveen Saini
Er Parveen Saini

Reputation: 95

How to make email unique in yii user model

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

Upvotes: 6

Views: 10989

Answers (3)

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

Upvotes: 14

ahmed
ahmed

Reputation: 867

public function rules()
{
    return array(
        ...
        array('email', 'email'),
        array('email', 'unique'),
        ...
    );
}

Upvotes: 6

Krishna
Krishna

Reputation: 1540

Try this:

As per your given code, It seems ok

You can verify using this url about validation:

http://www.yiiframework.com/doc/guide/1.1/en/form.model#declaring-validation-rules

Upvotes: 0

Related Questions