How to validate email and email already exist or not check, in Yii Framework?

后端 未结 6 994
日久生厌
日久生厌 2020-12-18 21:15

How to validate email using Yii Model validation rules function code. Also how to check email exist or not using Model validation rules function in Yii.

6条回答
  •  無奈伤痛
    2020-12-18 21:31

    Follow the few modifications as below: Follow your file as per your module you have used.

    Go to models -> open-> Users.php -> Modify line as mentioned below.

     public function rules()
        {
            return [
                    [['User_Email'], 'unique'],
                    [['User_Mobile'],'unique'],
                  ];
         }
    

    Now Go to views-> users ->Open _form.php-> write the code as mentioned below

    $model->formName(), 'enableAjaxValidation' => true, ]); ?> field($model, 'User_Email')->textInput(['maxlength' => true])?> field($model, 'User_Mobile')->textInput(['maxlength' => true])?>
    isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    Now Go to Controller->open UsersController.php -> wirte the code as mentioned below

    public function actionCreate()
        {
           if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())){
                Yii :: $app->response->format = 'json';
                return \yii\bootstrap\ActiveForm::validate($model);
                }
    }
    

    Thank you

提交回复
热议问题