0

I have form in yii that validates the form field. When I submit the form it shows the errors. But when the value of the field with the validation error is updated, the error still present. I want the message to clear. How should i clear the validation error?

Below the form widget code

<?php $form = $this->beginWidget('CActiveForm', array(
    'id'=>'user-form',
    'enableAjaxValidation'=>true
)); ?>

In my form I echo the validation error like the code below:

 <?php echo $form->error($model, 'firstname'); ?> 

I tried the solution from this problem Trigger Yii field validation onchange of another field

$('#user-form').change(function(){

    var settings = $(this).data('settings');
    $.each(settings.attributes, function () {
        this.status = 2; // force ajax validation
    });
    $(this).data('settings', settings);

    // trigger ajax validation
    $.fn.yiiactiveform.validate($(this), function (data) {
        $.each(settings.attributes, function () {
            $.fn.yiiactiveform.updateInput(this, data, $(this));
        });

    });
});

But the error message is still not cleared. I have confirmed that the ajax request is sent and there are response as its showed on the firebug console.

[EDIT] It seems those validation errors for "select" fields are the ones that are not updated/cleared only.

[EDIT] All the validation errors that are printed/echo after the form is submmitted will not disappear even if the value is supplied or change to satisfy the validation rules.

Community
  • 1
  • 1
Pelang
  • 421
  • 7
  • 19

3 Answers3

1

Place this just above the // trigger ajax validation comment:

$('.errorSummary, .errorMessage').hide();

This should reset the errors before they get re-validated.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
  • will it not removed also the fields that should have validation erros also? – Pelang Jun 11 '13 at 03:00
  • i think we should have the hide() since removeClass will not hide the error text. and add removclass only for the fields. thanks for response. but my concern is that the fields which values does not meet the validation, its validation errors will also be remove. – Pelang Jun 11 '13 at 03:12
1

In my case I added the code below on the page to remove the red highlight on input field on the form.

$('#user-form select, #user-form input').change(function(){

    field = $(this).attr('id');

    if($('#'+field+'_em').text() == ''){
        $(this).removeClass('error');
    }
});

I also add the updateInput function on framework/web/js/source/jquery.yiiactiveform.js so it will remove the validation error for certain field.

if(hasError == false){
    $error.toggle(hasError);
    $el2 = form.find('#' + attribute.id);
    $el2.removeClass(attribute.errorCssClass);
}

I am not sure if this the proper solution but it works for me.

Pelang
  • 421
  • 7
  • 19
0

With your form widget code, i don't think ajax validation won't work.

To enable Ajax validation on form you should configure your widget as below,

<?php
$form = $this->beginWidget('CActiveForm', array(
    'id'=>'user-form',
    'enableAjaxValidation' => true,
    'clientOptions' => array(
        'validateOnSubmit' => true,
    ),
        ));
?>

With this configuration, your form will be validated when form field lost the focus(blur).

Try it, It will give the solution for your problem.

Hearaman
  • 8,466
  • 13
  • 41
  • 58