yii2: enableClientValidation not work on submitting if set enableAjaxValidation to true
What steps will reproduce the problem?
My model:
class SignupForm extends \yii\base\Model
{
public $email;
public $password;
public function rules()
{
return [
['email', 'required'],
['email', 'email'],
['email', 'unique', 'targetClass' => User::className()],
['password', 'required'],
];
}
public function signup()
{
if (!$this->validate()) {
return null;
}
$user = new User;
$user->email = $this->email;
$user->setPassword($this->password);
return $user->save();
}
}
My controller action:
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ($model->signup()) {
return $this->goHome();
}
}
return $this->render('signup', [
'model' => $model,
]);
}
My form:
<?php
$form = ActiveForm::begin([
'id' => 'form-signup',
'action' => ['site/signup'],
'enableAjaxValidation' => true,
]); ?>
<?= $form->field($model, 'email', ['inputOptions' => ['autofocus' => 'autofocus']])->input('email') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= Html::submitButton('Signup') ?>
<?php ActiveForm::end(); ?>
What is the expected result?
Validate all supported client validations before sending ajax validation request
What do you get instead?
Client validation work correctly on input blur, change event, but send ajax request on submitting although all fields are empty.
Additional info
| Q | A |
|---|---|
| Yii version | 2.0.12 |
| PHP version | 7.1.10 |
| Operating system | Debian |
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 1
- Comments: 17 (12 by maintainers)
@machour perhaps I have doubts either in the way of correcting or in reproducing the error. Now I can not remember exactly