I have had this form for some days now, some fields use ajax validation. As production is near, today I started testing this form again and to my surprise it is submitting via ajax instead of normal post request.
view file:
<section class="bg-light p-4">
<h1><?= Html::encode($this->title) ?></h1>
<p><?= Yii::t('app', 'p_signup') ?>:</p>
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'type')->radioList([0 => 'Particular', 1 => 'Company'], ['class' => 'type']) ?>
<?= $form->field($model, 'name', ['enableAjaxValidation' => true])->textInput() ?>
<?= $form->field($model, 'company', ['enableAjaxValidation' => true])->textInput() ?>
<?php if (Yii::$app->user->can('createStaff')) {
echo $form->field($model, 'username', ['enableAjaxValidation' => true])->textInput();
} ?>
<?= $form->field($model, 'email', ['enableAjaxValidation' => true])->input('email', ['autoComplete' => 'off']) ?>
<input type="text" disabled hidden>
<?= $form->field($model, 'password')->passwordInput(['autocomplete' => 'off']) ?>
<?= $form->field($model, 'confirm_password')->passwordInput(['autocomplete' => 'off']) ?>
<div class="form-group">
<?= Html::submitButton(Yii::t('form', 'b_signup'), ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</section>
Controller action:
public function actionSignup()
{
$model = new SignupForm();
$post = Yii::$app->request->post();
if (Yii::$app->request->isAjax && $model->load($post)) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ($model->load($post) && $model->signup()) {
Yii::$app->session->setFlash('success', 'Thank you for registration. Please check your inbox for verification email.');
return $this->goHome();
}
return $this->render('signup', [
'model' => $model
]);
}
The culprit seems to be Yii client-side scripts:
send
https://static.example.com/www/assets/96c771a0/jquery.js:10099:10
ajax
https://static.example.com/www/assets/96c771a0/jquery.js:9682:15
validate/<
https://static.example.com/www/assets/abd67956/yii.activeForm.js:379:23
fire
https://static.example.com/www/assets/96c771a0/jquery.js:3496:31
add
https://static.example.com/www/assets/96c771a0/jquery.js:3555:7
always
https://static.example.com/www/assets/96c771a0/jquery.js:3708:15
validate
https://static.example.com/www/assets/abd67956/yii.activeForm.js:366:43
submitForm
https://static.example.com/www/assets/abd67956/yii.activeForm.js:441:34
dispatch
https://static.example.com/www/assets/96c771a0/jquery.js:5429:27
add/elemData.handle
https://static.example.com/www/assets/96c771a0/jquery.js:5233:28
(Async: EventListener.handleEvent) add
https://static.example.com/www/assets/96c771a0/jquery.js:5281:12
on/<
https://static.example.com/www/assets/96c771a0/jquery.js:5181:16
each
https://static.example.com/www/assets/96c771a0/jquery.js:381:19
each
https://static.example.com/www/assets/96c771a0/jquery.js:203:17
on
https://static.example.com/www/assets/96c771a0/jquery.js:5180:14
on
https://static.example.com/www/assets/96c771a0/jquery.js:5920:10
init/<
https://static.example.com/www/assets/abd67956/yii.activeForm.js:232:27
each
https://static.example.com/www/assets/96c771a0/jquery.js:381:19
each
https://static.example.com/www/assets/96c771a0/jquery.js:203:17
init
https://static.example.com/www/assets/abd67956/yii.activeForm.js:198:25
$.fn.yiiActiveForm
https://static.example.com/www/assets/abd67956/yii.activeForm.js:18:33
<anonymous>
https://www.example.com/user/signup:531:15
mightThrow
https://static.example.com/www/assets/96c771a0/jquery.js:3762:29
resolve/</process<
https://static.example.com/www/assets/96c771a0/jquery.js:3830:12
(Async: setTimeout handler) resolve/<
https://static.example.com/www/assets/96c771a0/jquery.js:3868:16
fire
https://static.example.com/www/assets/96c771a0/jquery.js:3496:31
fireWith
https://static.example.com/www/assets/96c771a0/jquery.js:3626:7
fire
https://static.example.com/www/assets/96c771a0/jquery.js:3634:10
fire
https://static.example.com/www/assets/96c771a0/jquery.js:3496:31
fireWith
https://static.example.com/www/assets/96c771a0/jquery.js:3626:7
ready
https://static.example.com/www/assets/96c771a0/jquery.js:4106:13
completed
https://static.example.com/www/assets/96c771a0/jquery.js:4116:9
(Async: EventListener.handleEvent) <anonymous>
https://static.example.com/www/assets/96c771a0/jquery.js:4132:11
<anonymous>
https://static.example.com/www/assets/96c771a0/jquery.js:36:10
<anonymous>
https://static.example.com/www/assets/96c771a0/jquery.js:40:4
Can I get some help? Thanks.
Edit: I’m pretty sure this is caused by some changes in the last update, as all forms are now sent via ajax. The problem is, when you’re using ajax validation this becomes a problem cuz it makes it impossible to process the request.