I was trying to validate my YII2 register form but it not work. In view:
$form = ActiveForm::begin([
'id' => 'register',
'options' => ['accept-charset'=>'utf-8'],
'validateOnChange' => false,
'enableAjaxValidation' => true,
'validateOnSubmit' => true,
])
In controller:
$model = new MUser();
if($model->load(Yii::$app->request->post()) && Yii::$app->request->isAjax)
{
$model->refresh();
Yii::$app->response->format = 'json';
return ActiveForm::validate($model);
}
elseif($model->load(Yii::$app->request->post()) && $model->save())
{
\\do something
}
In Model:
public function rules()
{
return [
[
'username',
'unique',
'targetClass' => 'com\modules\admin\models\MUser',
'message' => 'Username exist',
]
];
}
Can anyone let me know what wrong I am doing?
Aucun commentaire:
Enregistrer un commentaire