Ajax Validation - Customize Errorsummary

I recently created this issue but it was closed: https://github.com/yiisoft/yii/issues/2979

Basically I was wondering whether we could simply display a generic "there are errors - please correct" message instead of the full message which also lists every error.

Does anyone know of a way something like this can be easily achieved?

You got there a suggestion how to do it:




<?php if ($model->hasErrors()): ?> 

<div class=”error-summary”>My fascinating error summary without any error message</div>

 <?php endif; ?>

Yeah but that doesn’t work for ajax validation. For ajax validation we need to use $form->errorSummary().

I don’t understand why would errorSummary work on ajax validation and the above snippet not… both are rendered in the view… and btw. check the errorSummary source, it uses $model->getErrors().

Try it… It didn’t work for me.

You are right, I mixed the activeform and gridview way of updating the page.

It’s a bit tricky but it can still be done, you just need to “manually” write the DIV block that errorSummary() does.

errorSummary sets the ID of it’s block to the ID of the activeform + “es”, and hides it’s DIV initialy, so this code will work:




<div id="<?php echo $form->id ?>_es_" class="errorSummary" style="display:none">

    My fascinating error summary without any error message

</div>




I just tried that now - unfortunately that does not work either.

It should work as this way we are copying the errorSummary()… also I tested it before posting…

Try to put the errorSummary() in your view and check the DIV it generates, should be the same as the manually added code above just with a different message (default one)

Definitely does not work. I tested it and found that when you include:


$form->errorSummary($model);

the following is generated in the JS code at the bottom of the page:


'summaryID':'register-form_es_'

I tested in version 1.1.14.

Nice find! yes it worked for me because I had still the errorSummary call so the JS line was there but only the manual DIV was shown because of duplicate ID’s… so not a good solution :(

Found a simpler way to achieve your need. As all the error messages are shown in a UL, just use the errorSummary() with a custom header and hide the UL with CSS





.errorSummary ul{

	display:none;

}



Thanks. Yeah that’s a good solution. Still I prefer if we just had a simple option to enable/disable listing all errors. I think this would be a reasonable request?

If some functionality is not needed frequently and there is a way to achieve the desired result I think it’s better to not be in the core. So it all relates on how useful would that be but a[size=2]s you have seen nobody else commented on your github issue, so i would say this need is very rare.[/size]