yii2: possibility to show all error messages in activeForm

current implementation of client and server validation only show the first error message for an input. We have already control for this in model when setting skipOnError so if I set skipOnError in model I expect the error message to appear in the form.

It should be possible to configure/enable this behavior in ActiveForm.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 1
  • Comments: 17 (15 by maintainers)

Commits related to this issue

Most upvoted comments

this is not about errorSummary but about inputs in activeform.

@cebe @qiangxue @samdark

What do you think about creating an option to catch all errors that way?

helpers\BaseHtml.php

public static function errorSummary($models, $options = [])
{
    $header = isset($options['header']) ? $options['header'] : '<p>' . Yii::t('yii', 'Please fix the following errors:') . '</p>';
    $footer = ArrayHelper::remove($options, 'footer', '');
    $encode = ArrayHelper::remove($options, 'encode', true);
    unset($options['header']);

    $onlyFirst = isset($options['onlyFirst']) ? $options['onlyFirst'] : true;
    unset($options['onlyFirst']);

    $lines = [];
    if (!is_array($models)) {
        $models = [$models];
    }
    foreach ($models as $model) {
        /* @var $model Model */
        if ($onlyFirst === true) {
            foreach ($model->getFirstErrors() as $error) {
                $lines[] = $encode ? Html::encode($error) : $error;
            }
        } else {
            foreach ($model->getErrors() as $attribute => $errors) {
                foreach ($errors as $error) {
                    $lines[] = $encode ? Html::encode($error) : $error;
                }
            }
        }
    }

    if (empty($lines)) {
        // still render the placeholder for client-side validation use
        $content = '<ul></ul>';
        $options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none';
    } else {
        $content = '<ul><li>' . implode("</li>\n<li>", $lines) . '</li></ul>';
    }
    return Html::tag('div', $header . $content . $footer, $options);
}

no idea, could be in 2.0.x if it is configurable and default behavior does not change.