I’m using the yii2bootstrap4 plugin, and can;t get the error checking to show
in my view i have this
<?php
use yii\helpers\Html;
use yii\bootstrap4\ActiveForm;
use richardfan\widget\JSRegister;
use yii\helpers\Url;
$form = ActiveForm::begin([
'id' => $model->formName(),
'method' => 'post',
'enableAjaxValidation' => true,
'validateOnSubmit' => true,
'enableClientValidation'=> false,
'options' => ['autocomplete' => 'off'],
]);
?>
<?= $form->field($model, 'email')->textInput(['class' => 'form-control form-control-lg']); ?>
<?= $form->field($model, 'firstname')->textInput(['class' => 'form-control form-control-lg']); ?>
<?= $form->field($model, 'lastname')->textInput(['class' => 'form-control form-control-lg']); ?>
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary btn-block btn-lg shadow-sm']) ?>
<?php ActiveForm::end(); ?>
<script>
$(document).ready(function () {
var $form = $("#<?= $model->formName() ?>");
$form.on("submit", function (event, messages) {
event.preventDefault();
$.ajax({
"type":"POST",
"url":$form.attr('action'),
"data":$form.serialize(),
"beforeSend":function( xhr ) {
},
"dataType":"json",
"cache": true,
"success":function(data){
},
});
return false;
});
});
</script>
in my controller i have this
use yii\bootstrap4\ActiveForm;
use yii\helpers\Json;
use yii\web\Response;
public function actionCheck() {
//ajax validation
$model = new User;
$model->scenario = 'create';
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
if($model->validate()) {
$model = User::find()->where('id=:id', [':id' => Yii::$app->user->identity->id])->one();
$model->firstname = $_POST['User']['firstname'];
$model->lastname = $_POST['User']['lastname'];
$model->save();
} else {
Yii::$app->response->format = 'json';
return ActiveForm::validate($model);
}
Yii::$app->end();
}
return $this->render('check', [
'model' => new User,
]);
}
the returned JSON if all fields are empty is this
{"user-email":["Email cannot be blank."],"user-firstname":["Firstname cannot be blank."],"user-lastname":["Lastname cannot be blank."]}
any idea what im missing here? Thanks