Ajax Validation For Selected Attributes

I am using CActiveForm and ajax validation to validate a multi-step form (using jQuery show/hide divs).

In my controller action I specify which attributes to validate on each step, for example:


if(isset($_POST['step1'], $_POST['Listing']))

{

	$attributes = array('company', 'address1', 'town', 'phone1', 'email');

	

	$this->performAjaxValidation($model, $attributes);

}

This works fine in that it does not give validation errors for inputs that are on other steps. However when the validation is successful and I move to the next step, all the inputs have a green "success" background on them.

Obviously this is not correct at this point. How can I fix this?

Anybody got any idea about this? Is this a bug?

If you don’t need this green style u can set


'clientOptions' => array(

    'successCssClass' => null,

),

in widget params

I see. That does indeed take off the green background. However the green background is useful to know which attributes passed validation.

Do you think this is a bug? The way I see it, any attributes that are not specified for validation should not get the green background.

I don’t think this is a bug. It’s because AJAX validation return in JSON only invalid fields. In that case ActiveForm’s javascript callback function assume that all fields which are not listed in response are valid.

I have another idea. You can wrap each step with different form (each with same action). Then validation should change only current form. I know it’s quite “ugly” but sometimes we have to make sacrifices ;). Maybe removing green background in afterValidate function would be better solution for you.

ActiveForm’s JS code keeps a state of each field. On page load all fields are marked as not checked and even when validating whole form they should not be marked either as valid or not. Only after being modified by user (or getting focused) validation assigns css classes to them.

Check if you don’t call change() or focus() event on fields that haven’t been yet filled by the user. Maybe you clear the whole set of fields when advancing to the next step?