2012-09-07 23 views
5

mã xác nhận làLàm thế nào để làm cho email duy nhất trong mô hình sử dụng 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'), 
    ); 
+0

Tôi nghĩ rằng cú pháp do bạn là hoàn hảo. – Krishna

Trả lời

13

Bạn có thể làm cho email của mình trở thành duy nhất trong mô hình người dùng yii theo quy tắc sau như được đưa ra bên dưới.

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

Đây className là tên của lớp mô hình người dùng, attributeName là tên trường email db của bạn.

Bạn cũng có thể kiểm tra liên kết bên dưới.

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

Cảm ơn

+0

Câu trả lời hay, Cần lưu ý rằng chúng ta nên sử dụng các kịch bản để quy tắc cụ thể này không áp dụng khi người dùng đăng nhập ... sử dụng: ''on' => 'register'' – surfer190

3
public function rules() 
{ 
    return array(
     ... 
     array('email', 'email'), 
     array('email', 'unique'), 
     ... 
    ); 
} 
0
array('email', 'filter', 'filter'=>'trim'), 
     array('email', 'unique'), 
Các vấn đề liên quan