mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
Fixes #7988: Added \yii\helpers\Console::errorSummary()
and \yii\helpers\Json::errorSummary()
This commit is contained in:

committed by
Alexander Makarov

parent
99030465ff
commit
f11f818b18
@ -8,6 +8,7 @@
|
||||
namespace yii\helpers;
|
||||
|
||||
use yii\console\Markdown as ConsoleMarkdown;
|
||||
use yii\base\Model;
|
||||
|
||||
/**
|
||||
* BaseConsole provides concrete implementation for [[Console]].
|
||||
@ -1051,4 +1052,45 @@ class BaseConsole
|
||||
self::$_progressEtaLastDone = 0;
|
||||
self::$_progressEtaLastUpdate = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a summary of the validation errors.
|
||||
* @param Model|Model[] $models the model(s) whose validation errors are to be displayed.
|
||||
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
|
||||
*
|
||||
* - showAllErrors: boolean, if set to true every error message for each attribute will be shown otherwise
|
||||
* only the first error message for each attribute will be shown. Defaults to `false`.
|
||||
*
|
||||
* @return string the generated error summary
|
||||
* @since 2.0.14
|
||||
*/
|
||||
public static function errorSummary($models, $options = [])
|
||||
{
|
||||
$showAllErrors = ArrayHelper::remove($options, 'showAllErrors', false);
|
||||
$lines = self::collectErrors($models, $showAllErrors);
|
||||
|
||||
return implode(PHP_EOL, $lines);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return array of the validation errors
|
||||
* @param Model|Model[] $models the model(s) whose validation errors are to be displayed.
|
||||
* @param $showAllErrors boolean, if set to true every error message for each attribute will be shown otherwise
|
||||
* only the first error message for each attribute will be shown.
|
||||
* @return array of the validation errors
|
||||
* @since 2.0.14
|
||||
*/
|
||||
private static function collectErrors($models, $showAllErrors)
|
||||
{
|
||||
$lines = [];
|
||||
if (!is_array($models)) {
|
||||
$models = [$models];
|
||||
}
|
||||
|
||||
foreach ($models as $model) {
|
||||
$lines = array_unique(array_merge($lines, $model->getErrorSummary($showAllErrors)));
|
||||
}
|
||||
|
||||
return $lines;
|
||||
}
|
||||
}
|
||||
|
@ -1216,26 +1216,7 @@ class BaseHtml
|
||||
$encode = ArrayHelper::remove($options, 'encode', true);
|
||||
$showAllErrors = ArrayHelper::remove($options, 'showAllErrors', false);
|
||||
unset($options['header']);
|
||||
|
||||
$lines = [];
|
||||
if (!is_array($models)) {
|
||||
$models = [$models];
|
||||
}
|
||||
foreach ($models as $model) {
|
||||
/* @var $model Model */
|
||||
foreach ($model->getErrors() as $errors) {
|
||||
foreach ($errors as $error) {
|
||||
$line = $encode ? Html::encode($error) : $error;
|
||||
if (!in_array($line, $lines, true)) {
|
||||
$lines[] = $line;
|
||||
}
|
||||
if (!$showAllErrors) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$lines = self::collectErrors($models, $encode, $showAllErrors);
|
||||
if (empty($lines)) {
|
||||
// still render the placeholder for client-side validation use
|
||||
$content = '<ul></ul>';
|
||||
@ -1247,6 +1228,35 @@ class BaseHtml
|
||||
return Html::tag('div', $header . $content . $footer, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return array of the validation errors
|
||||
* @param Model|Model[] $models the model(s) whose validation errors are to be displayed.
|
||||
* @param $encode boolean, if set to false then the error messages won't be encoded.
|
||||
* @param $showAllErrors boolean, if set to true every error message for each attribute will be shown otherwise
|
||||
* only the first error message for each attribute will be shown.
|
||||
* @return array of the validation errors
|
||||
* @since 2.0.14
|
||||
*/
|
||||
private static function collectErrors($models, $encode, $showAllErrors)
|
||||
{
|
||||
$lines = [];
|
||||
if (!is_array($models)) {
|
||||
$models = [$models];
|
||||
}
|
||||
|
||||
foreach ($models as $model) {
|
||||
$lines = array_unique(array_merge($lines, $model->getErrorSummary($showAllErrors)));
|
||||
}
|
||||
|
||||
if ($encode) {
|
||||
for ($i = 0; $i < count($lines); $i++) {
|
||||
$lines[$i] = Html::encode($lines[$i]);
|
||||
}
|
||||
}
|
||||
|
||||
return $lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a tag that contains the first validation error of the specified model attribute.
|
||||
* Note that even if there is no validation error, this method will still return an empty error tag.
|
||||
|
@ -10,6 +10,7 @@ namespace yii\helpers;
|
||||
use yii\base\Arrayable;
|
||||
use yii\base\InvalidParamException;
|
||||
use yii\web\JsExpression;
|
||||
use yii\base\Model;
|
||||
|
||||
/**
|
||||
* BaseJson provides concrete implementation for [[Json]].
|
||||
@ -179,4 +180,45 @@ class BaseJson
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a summary of the validation errors.
|
||||
* @param Model|Model[] $models the model(s) whose validation errors are to be displayed.
|
||||
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
|
||||
*
|
||||
* - showAllErrors: boolean, if set to true every error message for each attribute will be shown otherwise
|
||||
* only the first error message for each attribute will be shown. Defaults to `false`.
|
||||
*
|
||||
* @return string the generated error summary
|
||||
* @since 2.0.14
|
||||
*/
|
||||
public static function errorSummary($models, $options = [])
|
||||
{
|
||||
$showAllErrors = ArrayHelper::remove($options, 'showAllErrors', false);
|
||||
$lines = self::collectErrors($models, $showAllErrors);
|
||||
|
||||
return json_encode($lines);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return array of the validation errors
|
||||
* @param Model|Model[] $models the model(s) whose validation errors are to be displayed.
|
||||
* @param $showAllErrors boolean, if set to true every error message for each attribute will be shown otherwise
|
||||
* only the first error message for each attribute will be shown.
|
||||
* @return array of the validation errors
|
||||
* @since 2.0.14
|
||||
*/
|
||||
private static function collectErrors($models, $showAllErrors)
|
||||
{
|
||||
$lines = [];
|
||||
if (!is_array($models)) {
|
||||
$models = [$models];
|
||||
}
|
||||
|
||||
foreach ($models as $model) {
|
||||
$lines = array_unique(array_merge($lines, $model->getErrorSummary($showAllErrors)));
|
||||
}
|
||||
|
||||
return $lines;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user