yii2 unique 驗證


開發過程中會用到檢測用戶名或手機號或郵箱是否唯一。

在model的rules中加入

[['email'],'unique'],

在view中

<?php $form = ActiveForm::begin(['enableAjaxValidation' => true]); ?>

在controller中

public function actionCreate()
    {
        $model = new User();
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
}

即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM