我正在yii2項目中工作。我有用戶模型,該電子郵件ID應該是唯一的。在添加功能它正在工作。但是,當我更新記錄它不檢查獨特的驗證。如何在yii2中更新時檢查唯一驗證
以下是我的用戶模型代碼:
public function rules() {
return [
[['first_name', 'last_name', 'address', 'mobile', 'email', 'password_hash', 'role_id'], 'required'],
[['address'], 'string'],
[['role_id'], 'integer'],
[['email'], 'email'],
[['email'], 'unique', 'targetAttribute' => ['email']],
[['created_at', 'updated_at'], 'safe'],
[['first_name', 'last_name', 'email', 'password_hash'], 'string', 'max' => 255],
[['mobile'], 'required','on'=>'create,update'],
//[['mobile'], 'string','max'=>10],
[['mobile'], 'number','numberPattern' => '/^[0-9]{10}$/','message'=>"Mobile must be integer and should not greater then 10 digit"],
[['password_hash'],'string','min'=>6],
//[['mobile'], 'number'],
[['status'], 'string', 'max' => 1,'on'=>'create,update'],
[['role_id'], 'exist', 'skipOnError' => true, 'targetClass' => Roles::className(), 'targetAttribute' => ['role_id' => 'id']],
];
}
即使我嘗試:[[ '電子郵件'], '獨一無二', 'targetAttribute'=> [ '電子郵件'], '上' =>數組('創建','更新')],
但沒有發生它不檢查更新函數中的唯一規則。
您不必添加'targetAttribute'如果它是與選中的一樣。你爲什麼認爲它不起作用? – Bizley
是的,我刪除了它仍然不工作更新頁面 – David
再次 - 爲什麼你認爲它不工作?解釋你期望的是什麼,取而代之的是什麼。 – Bizley