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.
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,
]); ?>
= $form->field($model, 'User_Email')->textInput(['maxlength' => true])?>
= $form->field($model, 'User_Mobile')->textInput(['maxlength' => true])?>
= Html::submitButton($model->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