From a8c0b23e0c7a776a37a835834a419a031878fc7b Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Fri, 12 Jan 2018 15:16:48 +0200 Subject: [PATCH] `yii\bootstrap\Html::error()` now automatically set 'help-block help-block-error' CSS class for generated tag --- ActiveField.php | 4 ++-- BaseHtml.php | 15 +++++++++++++++ CHANGELOG.md | 1 + tests/ActiveFieldTest.php | 4 ++-- tests/HtmlTest.php | 11 +++++++++++ 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/ActiveField.php b/ActiveField.php index 0817471..11dd10c 100644 --- a/ActiveField.php +++ b/ActiveField.php @@ -385,8 +385,8 @@ class ActiveField extends \yii\widgets\ActiveField $config['horizontalCssClasses'] = $cssClasses; $config['wrapperOptions'] = ['class' => $cssClasses['wrapper']]; $config['labelOptions'] = ['class' => 'control-label ' . $cssClasses['label']]; - $config['errorOptions'] = ['class' => 'help-block help-block-error ' . $cssClasses['error']]; - $config['hintOptions'] = ['class' => 'help-block ' . $cssClasses['hint']]; + $config['errorOptions']['class'] = 'help-block help-block-error ' . $cssClasses['error']; + $config['hintOptions']['class'] = 'help-block ' . $cssClasses['hint']; } elseif ($layout === 'inline') { $config['labelOptions'] = ['class' => 'sr-only']; $config['enableError'] = false; diff --git a/BaseHtml.php b/BaseHtml.php index 106102c..2e76f5c 100644 --- a/BaseHtml.php +++ b/BaseHtml.php @@ -127,4 +127,19 @@ class BaseHtml extends \yii\helpers\Html return parent::checkboxList($name, $selection, $items, $options); } + + /** + * {@inheritdoc} + * @since 2.0.8 + */ + public static function error($model, $attribute, $options = []) + { + if (!array_key_exists('tag', $options)) { + $options['tag'] = 'p'; + } + if (!array_key_exists('class', $options)) { + $options['class'] = 'help-block help-block-error'; + } + return parent::error($model, $attribute, $options); + } } diff --git a/CHANGELOG.md b/CHANGELOG.md index 39a8143..058d2bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Yii Framework 2 bootstrap extension Change Log - Bug #126: Fixed `yii\bootstrap\ToggleButtonGroup` toggles dropdown for both buttons in case `split` is enabled (klimov-paul) - Bug #136: Allow overriding `horizontalCssClasses` when extending `\yii\bootstrap\ActiveField` (mikehaertl, klimov-paul) - Enh #118: Methods `radioList()` and `checkboxList()` overridden at `yii\bootstrap\Html` applying Bootstrap style (klimov-paul) +- Enh #148: `yii\bootstrap\Html::error()` now automatically set 'help-block help-block-error' CSS class for generated tag (klimov-paul) - Enh #219: Add ability to use custom HTML in navbar-header (razvanphp) - Enh #171: Add ability to use a brandImage with the navbar (razvanphp) - Enh #227: Added `yii\bootstrap\Collapse::$itemToggleOptions` allowing setup custom collapse tag name and HTML options (mskayali, klimov-paul) diff --git a/tests/ActiveFieldTest.php b/tests/ActiveFieldTest.php index a2b71b0..6b6a86d 100644 --- a/tests/ActiveFieldTest.php +++ b/tests/ActiveFieldTest.php @@ -126,7 +126,7 @@ HTML;
-
+

@@ -151,7 +151,7 @@ EXPECTED;
-
+

diff --git a/tests/HtmlTest.php b/tests/HtmlTest.php index 309c99f..f1e9c97 100644 --- a/tests/HtmlTest.php +++ b/tests/HtmlTest.php @@ -2,6 +2,7 @@ namespace yiiunit\extensions\bootstrap; +use yii\base\DynamicModel; use yii\bootstrap\Html; /** @@ -153,4 +154,14 @@ EOD; EOD; $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', 'value', ['value' => 'label&'], ['encode' => false])); } + + public function testError() + { + $model = new DynamicModel(); + $model->addError('foo', 'Some error message.'); + + $this->assertEquals('

Some error message.

', Html::error($model, 'foo')); + $this->assertEquals('

Some error message.

', Html::error($model, 'foo', ['class' => 'custom-class'])); + $this->assertEquals('
Some error message.
', Html::error($model, 'foo', ['tag' => 'div'])); + } } \ No newline at end of file