CActiveForm errorSummary()

I have two models on my form - enquiry and user

I am using AJAX Validation and in my controller I have the following:


if(isset($_POST['ajax']) && $_POST['ajax']==='enquiry-user-form')

{

	echo CActiveForm::validate(array($enquiry, $user));

	Yii::app()->end();

}

In my view I have the following:


<?php echo $form->errorSummary($enquiry); ?>

I.e. I only want errorSummary to display errors for enquiry model. However what is happening is that the errors for user model are also being displayed. I have validateOnSubmit option enabled.

For example here is one of my ‘user’ fields on the form:


<div class="row">

        <?php echo $form->labelEx($user, 'email_address'); ?>

        <?php echo $form->textField($user, 'email_address', array('size'=>25)); ?>

	<?php echo $form->error($user, 'email_address'); ?>

</div>

Is this a bug or have I done something incorrectly?

I think it might have something to do with


echo CActiveForm::validate(array($enquiry, $user));

try removing $user.

No well if I remove $user then I don’t get AJAX validation for the user model fields!

I still want the validation for the user model, I just don’t want their errors to be displayed in the errorSummary()!

You were not clear if you still wanted the user to validate or not!

Anyway, I think you need to look at the afterValidate callback and create a custom javascript function to handle the errors.

more info @ http://www.yiiframework.com/doc/api/CActiveForm#clientOptions-detail

Yeah but…

http://www.yiiframework.com/doc/api/CActiveForm#errorSummary-detail


public string errorSummary(mixed $models, string $header=NULL, string $footer=NULL, array $htmlOptions=array ( ))

$models - mixed - the models whose input errors are to be displayed. This can be either a single model or an array of models.

That’s what I have done - I specified the model so I expected only the enquiry model errors to be displayed… so this does look like a bug!

Your mistaken <?php echo $form->errorSummary($enquiry); ?> has nothing to do with the AJAX request. It does however generate a hidden div to populate via Javascript.


<div id="user-form_es_" class="errorSummary" style="display:none"><p>Please fix the following input errors:</p>

<ul><li>dummy</li></ul></div>

Which depending on the clientOptions might get used.

If you inspect the AJAX response you should see a JSON object e.g.


{"User_email":["Email cannot be blank."],"User_new_password":["Password cannot be blank."],"User_new_password_confirmation":["Password Confirmation cannot be blank."],"User_role":["Role cannot be blank."]}

which will be generated by the echo CActiveForm::validate(array($enquiry, $user)); method (obviously with different error messages).

This then gets used by the jquery activeform plugin (look in jquery.yiiactiveform.js) to display the error messages, I think there is a "updateSummary" method in there (had a look at it a while back). This method then tagets some hidden divs to show the error message/s e.g. <div id="User_email_em_" class="errorMessage" style="display:none"></div> which get generated when you enable ajax validation!

I’m sure it does, because I have validateOnSubmit enabled, so as soon as I submit the form the errorSummary div gets displayed.

Thats the first time you have metioned having validateOnSubmit enabled, which might behave differently I think! well I hope its useful to someone else…if not you.

edit: gonna give it a quick test

I mentioned it in my original post.

If I missed it my apologies…

Anyway after a quick test! my orignal answer is correct to my knowledge, let me quote myself.

It does however generate a hidden div to populate via Javascript.

you’ll get there eventually!

Anyone else able to replicate this problem?

there is no problem to replicate ::) but I sure don’t mind being corrected by someone… :)

Is not a bug, and nothing is incorrect.

The fact is that all this system has been built for act in the same way with fields and with errorsummary.

What the js function is doing is to take ALL error and display and in error summary and in fields.

The part of error summary is not checking the model from wich the errors arrives, so, as far as I can immagine, there are no possible solutions.

You can write in feature request and explain your idea, maybe in future thew will implement.

In that case then how come there is a $models parameter of errorSummary?

($models - mixed - the models whose input errors are to be displayed. This can be either a single model or an array of models.)

I can see what your trying to do…

I think if you disable AJAX it should give you the desired effect.

The errorSummary method only creates a placeholder div for the AJAX request to work with, it does nothing else! that is unless AJAX is off, only then does it show error messages for the models you pass to it.

When AJAX is enabled, the div gets populated via the "updateSummary" callback which occurs after the AJAX request (look in jquery.yiiactiveform.js).

This array of models are used for display errors in post methods, but when you are using ajax validation, it is not making any distinction.

From this point of view, is an unexpected behavior (or a bug). Possible fix can be to change the ajax validation response in order to add the name of the model, for example, and to use this name for filter the errors in the js function that fill the error summary.

Right now:

  • there is no such an id

  • ther is no possiblity to add the filter (the function beforeValidat runs before the validation is submitted, and the afterValidat after all errors are already displaied).

We could use the the afterValidate if the <li> of the error summary were create with an id/class or some other way to recognize the model generate this error, but this is also not possible, they are plane li.

I don’t see any option, expect inspecthing the code of CActiveForm and extend it.

You can try with something like




if(isset($_POST['ajax']) && $_POST['ajax']==='enquiry-user-form')

{

        CActiveForm::validate(array($user));

    	echo CActiveForm::validate(array($enquiry));

    	Yii::app()->end();

}

This way you do the validation of user but you do not output the errors, and validate the enquiry model and output possible errors only for this model…