Browse Source

Merge branch 'PowerGamer1-patch-11'

tags/2.0.10
SilverFire - Dmitry Naumenko 8 years ago
parent
commit
bb7a36c5c6
  1. 1
      framework/CHANGELOG.md
  2. 15
      framework/helpers/BaseHtml.php
  3. 95
      tests/framework/helpers/HtmlTest.php

1
framework/CHANGELOG.md

@ -8,6 +8,7 @@ Yii Framework 2 Change Log
- Bug #12428: Fixed `yii\db\mysql\QueryBuilder` causes warning when insert default rows into a table without primary key (DrmagicE)
- Enh #9989: ActiveForm now respects formtarget, formmethod and formenctype attributes of submit button (AnatolyRugalev)
- Enh #12296: Added value validation to `yii\log\Target::setLevels()` (Mak-Di)
- Enh #12193: Added the ability to suppress the generation of duplicate error messages in `Html::errorSummary`. Added the ability to display error messages beyond the first error for each model attribute (PowerGamer1)
- Enh #12073: Added the ability to suppress the generation of input hint when it is specified through `Model::attributeHints()` (PowerGamer1)
- Bug #7670: Added `UrlNormalizer` for normalizing requests with and without trailing slashes (rob006, cronfy, klimov-paul)
- Bug #9277: Fixed `yii\console\controllers\AssetController` looses custom options of 'target' bundles (petrabarus, klimov-paul)

15
framework/helpers/BaseHtml.php

@ -1154,6 +1154,8 @@ class BaseHtml
* - header: string, the header HTML for the error summary. If not set, a default prompt string will be used.
* - footer: string, the footer HTML for the error summary.
* - encode: boolean, if set to false then the error messages won't be encoded.
* - 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.
*
* The rest of the options will be rendered as the attributes of the container tag. The values will
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
@ -1164,6 +1166,7 @@ class BaseHtml
$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);
$showAllErrors = ArrayHelper::remove($options, 'showAllErrors', false);
unset($options['header']);
$lines = [];
@ -1172,8 +1175,16 @@ class BaseHtml
}
foreach ($models as $model) {
/* @var $model Model */
foreach ($model->getFirstErrors() as $error) {
$lines[] = $encode ? Html::encode($error) : $error;
foreach ($model->getErrors() as $errors) {
foreach ($errors as $error) {
$line = $encode ? Html::encode($error) : $error;
if (array_search($line, $lines) === false) {
$lines[] = $line;
}
if (!$showAllErrors) {
break;
}
}
}
}

95
tests/framework/helpers/HtmlTest.php

@ -3,6 +3,7 @@
namespace yiiunit\framework\helpers;
use Yii;
use yii\base\DynamicModel;
use yii\base\Model;
use yii\helpers\Html;
use yii\helpers\Url;
@ -881,6 +882,82 @@ EOD;
$this->assertEquals($expectedHtml, Html::activePasswordInput($model, 'name', $options));
}
public function errorSummaryDataProvider()
{
return [
[
'ok',
[],
'<div style="display:none"><p>Please fix the following errors:</p><ul></ul></div>'
],
[
'ok',
['header' => 'Custom header', 'footer' => 'Custom footer', 'style' => 'color: red'],
'<div style="color: red; display:none">Custom header<ul></ul>Custom footer</div>',
],
[
str_repeat('long_string', 60),
[],
'<div><p>Please fix the following errors:</p><ul><li>Name should contain at most 100 characters.</li></ul></div>'
],
[
'not_an_integer',
[],
'<div><p>Please fix the following errors:</p><ul><li>Error message. Here are some chars: &lt; &gt;</li></ul></div>',
function ($model) { /** @var $model DynamicModel */
$model->addError('name', 'Error message. Here are some chars: < >');
}
],
[
'not_an_integer',
['encode' => false],
'<div><p>Please fix the following errors:</p><ul><li>Error message. Here are some chars: < ></li></ul></div>',
function ($model) { /** @var $model DynamicModel */
$model->addError('name', 'Error message. Here are some chars: < >');
}
],
[
str_repeat('long_string', 60),
[],
'<div><p>Please fix the following errors:</p><ul><li>Error message. Here are some chars: &lt; &gt;</li></ul></div>',
function ($model) { /** @var $model DynamicModel */
$model->addError('name', 'Error message. Here are some chars: < >');
}
],
[
'not_an_integer',
['showAllErrors' => true],
'<div><p>Please fix the following errors:</p><ul><li>Error message. Here are some chars: &lt; &gt;</li>
<li>Error message. Here are even more chars: &quot;&quot;</li></ul></div>',
function ($model) { /** @var $model DynamicModel */
$model->addError('name', 'Error message. Here are some chars: < >');
$model->addError('name', 'Error message. Here are even more chars: ""');
}
],
];
}
/**
* @dataProvider errorSummaryDataProvider
*
* @param string $value
* @param array $options
* @param string $expectedHtml
* @param \Closure $beforeValidate
*/
public function testErrorSummary($value, array $options, $expectedHtml, $beforeValidate = null)
{
$model = new HtmlTestModel();
$model->name = $value;
if ($beforeValidate !== null) {
call_user_func($beforeValidate, $model);
}
$model->validate(null, false);
$this->assertEquals($expectedHtml, Html::errorSummary($model, $options));
}
/**
* Data provider for [[testActiveTextArea()]]
* @return array test data
@ -928,7 +1005,7 @@ EOD;
{
$model = new HtmlTestModel();
$model->description = $value;
$this->assertEquals($expectedHtml, Html::activeTextArea($model, 'description', $options));
$this->assertEquals($expectedHtml, Html::activeTextarea($model, 'description', $options));
}
/**
@ -966,11 +1043,19 @@ EOD;
}
}
class HtmlTestModel extends Model
/**
* @property string name
* @property array types
* @property string description
*/
class HtmlTestModel extends DynamicModel
{
public $name;
public $types;
public $description;
public function init()
{
foreach (['name', 'types', 'description'] as $attribute) {
$this->defineAttribute($attribute);
}
}
public function rules()
{

Loading…
Cancel
Save