From b3f54d106526975d25da8fe89d22bd5af9bcedcc Mon Sep 17 00:00:00 2001 From: SilverFire - Dmitry Naumenko Date: Sat, 10 Mar 2018 10:19:07 +0200 Subject: [PATCH] Simplified code, enhanced PHPDocs --- framework/CHANGELOG.md | 2 +- framework/helpers/BaseHtml.php | 9 ++++----- tests/framework/helpers/HtmlTest.php | 7 ++++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 262c6f5902..83c3654dfb 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 Change Log 2.0.14.2 under development ------------------------ -- Bug #15858: Fixed array undefined offset error calling Html::errorSummary with same error messages from different model attributes (FabrizioCaldarelli) +- Bug #15858: Fixed `Undefined offset` error calling `yii\helpers\Html::errorSummary()` with the same error messages for different model attributes (FabrizioCaldarelli, silverfire) - Bug #15783: Regenerate CSRF token only when logging in directly (samdark) - Bug #15801: Fixed `has-error` CSS class assignment in `yii\widgets\ActiveField` when attribute name is prefixed with tabular index (FabrizioCaldarelli) - Bug #15792: Added missing `yii\db\QueryBuilder::conditionClasses` setter (silverfire) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 05fbfcd4c9..fdf5b1b432 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -1255,14 +1255,13 @@ class BaseHtml $lines = array_unique(array_merge($lines, $model->getErrorSummary($showAllErrors))); } - // Fix #15858 (FlorinRo) - If there are same message errors for different attributes, array_unique could skip some array key, - // so the next for cycle could fail because key missing. - // Applying array_values reorder array keys. + // If there are the same error messages for different attributes, array_unique will leave gaps + // between sequential keys. Applying array_values to reorder array keys. $lines = array_values($lines); if ($encode) { - for ($i = 0, $linesCount = count($lines); $i < $linesCount; $i++) { - $lines[$i] = Html::encode($lines[$i]); + foreach ($lines as &$line) { + $line = Html::encode($line); } } diff --git a/tests/framework/helpers/HtmlTest.php b/tests/framework/helpers/HtmlTest.php index a2d07733d2..bc66921782 100644 --- a/tests/framework/helpers/HtmlTest.php +++ b/tests/framework/helpers/HtmlTest.php @@ -1320,8 +1320,9 @@ EOD; } /** - * Test that attributes that output same errors, return unique message error - */ + * Test that attributes that output same errors, return unique message error + * @see https://github.com/yiisoft/yii2/pull/15859 + */ public function testCollectError() { $model = new DynamicModel(compact('attr1', 'attr2')); @@ -1333,7 +1334,7 @@ EOD; $this->assertEquals( '

Please fix the following errors:

', - Html::errorSummary($model,['showAllErrors'=>true]) + Html::errorSummary($model, ['showAllErrors' => true]) ); }