Active Form Ajax Validation

The errors are not displayed when i validate a ActiveForm with AJAX. I tried this on a clean install of Yii 2.0.

My ActiveForm widget:

<?php $form = ActiveForm::begin([

        'enableClientValidation' => false,

        'enableAjaxValidation' => true

    ]); ?>

The controller is a default created by the CRUD generator.

The ajax request is made, the POST contains a ajax => w0 value, but in the response there is the whole page (including all HTML). The errors are displayed in there though. I expected a JSON format of errors. Did I forget something in the controller action? (something like $this->performAjaxValidation() in Yii 1.x)

Please refer to this:

Thanks, it’s working now. But how can i accomplish this with Html::error(); ? The error doesn’t shows up on AJAX validation.

Edit: I think it has something to do i don’t use the $form->field()->…() for this, but just Html::activeTextInput() for example. It doesn’t create the div around it then.

Yes, you’ll need to add more fields.


is basically a shortcut for

<?= Html::activeLabel($model, 'field'); ?>

<?= Html::activeTextInput($model, 'field'); ?>

<?= Html::error($model, 'field'); ?>

Yes i tried that already, but for some reason, validation with AJAX doesn’t assign the error to the error element. The validation is actually done by AJAX (the call is made).

AJAX response:

{"blacklist-value":["Value cannot be blank."]}

I think it has something to do with the fact that these fields don’t have the form-group, from-control html classes?

Ok, this approach works, but i’m not sure if there is an easier / better way to do this (i don’t want to use the default label => input => hint => error approach here):

<?php echo $form->field($model,'value')->begin(); ?>

<?php echo Html::activeLabel($model,'value'); ?>

<?php echo Html::activeTextInput($model,'value', ['maxlength'=>255, 'placeholder' => '123.456.78.9']); ?>

<?php echo Html::error($model,'value', ['class' => 'help-block']); ?>

<?php echo $form->field($model,'value')->end(); ?>

If you are using [font="Courier New"]ActiveField[/font] then you can edit the [font="Courier New"]template[/font] parameter to control which part to show/hide or add your own parts.

If you are not using [font="Courier New"]ActiveField[/font] but just plain [font="Courier New"]Html[/font] class to generate inputs, I feel, you may need to put in your own client validation Javascript (as it will not call the Yii default one).

I was thinking about the same when I designed the extension yii2-password. I could not use a simple InputWidget and generate form fields to generate the ajax validation the DEFAULT YII Way, with plain [font=“Courier New”]Html[/font] methods. So to use Yii’s inbuilt form validation javascripts, I used the [font=“Courier New”]ActiveField[/font] methods to generate the output and client validations. I configure the [font=“Courier New”]template[/font] property to control what part of the field I want to show.