Limit number of displayed validation errors


I’d like to limit the number of validation errors that are displayed. I do not want a user who submits an empty form to see 20 or so error messages, but, say, only the first 3.

I do not have write access to $form->errors. So how how could I accomplish this task?

Instead of using CHtml::errorSummary($model) in your view, You’re probably going to have to create your own error box and get the error array via $model->getErrors(). You can then create a forloop which iterates only three times, something like this:

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

 <div class="error">

  Please fix the following errors:



    $errors = $model->getErrors();



    <li><?php echo $errors[$i]; ?>

   <?php endfor; ?>



<?php endif; ?>

(Note I just typed this up so there are probably some issues with the code, but I think this should be your generic approach)

Thanks.Problem with your approach is that it only limits the displayed errors in the errorSummary while all errors are marked in the form.


It’s hard if you don’t have write access to the model’s errors, otherwise you could just remove all but the first three errors from the model manually. Maybe you should use normal input fields instead of active input fields, though that would mark no input fields at all. Is that a big problem?

You could override afterValidate() and do something like this:

  • Fetch array of errors with getErrrors()

  • Filter this array with some custom logic

  • Call clearErrors()

  • Add reduced list again with addErrors()

CHtml::errorSummary($model, $limit=0)

I think this is the best Solutions~

when you use CHtml::errorSummary($model, 3);

you can get the error message like:

following is the error message:

  1. xxx

  2. yyy

  3. zzz

Suggested that official to make this improvement

The problem above is not only about errorSummary but also the errors for each input field/label. Moreover how would you specify with your solition which 3 errors you want to have back? It’s not much use, if they come back randomly…

Thank you, Mike.

Thanks to your help, my solution now is

if (count($form->errors) > 3) {

  $err = $form->errors;


  $i = 1;

  foreach ($err as $key=>$val) {

    if ($i <= 3) {

      $form->addError($key, $val[0]);





However, I put it in in actionIndex just before rendering the page.

Add this in the model class

protected function afterValidate()



            $errors = $this->getErrors();


            $arr=array_chunk($errors, 3);