如何在yii用户模型中使电子邮件独特

时间:2012-09-07 11:15:08

标签: yii

验证码是

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

3 个答案:

答案 0 :(得分:13)

您可以通过以下规则在yii用户模型中使您的电子邮件成为唯一。

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

此处className是用户模型类的名称,attributeName是您的数据库电子邮件字段名称。

您也可以查看以下链接。

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

由于

答案 1 :(得分:3)

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

答案 2 :(得分:0)

试试这个:

根据你给出的代码,似乎没问题

您可以使用此网址验证验证:

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