Browse Source

`yii\bootstrap\Html::error()` now automatically set 'help-block help-block-error' CSS class for generated tag

tags/2.0.8
Klimov Paul 7 years ago
parent
commit
a8c0b23e0c
  1. 4
      ActiveField.php
  2. 15
      BaseHtml.php
  3. 1
      CHANGELOG.md
  4. 4
      tests/ActiveFieldTest.php
  5. 11
      tests/HtmlTest.php

4
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;

15
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);
}
}

1
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)

4
tests/ActiveFieldTest.php

@ -126,7 +126,7 @@ HTML;
<label class="control-label col-sm-3" for="dynamicmodel-attributename">Attribute Name</label>
<div class="col-sm-6">
<input type="text" id="dynamicmodel-attributename" class="form-control" name="DynamicModel[attributeName]">
<div class="help-block help-block-error "></div>
<p class="help-block help-block-error "></p>
</div>
</div>
@ -151,7 +151,7 @@ EXPECTED;
<label class="control-label col-md-4" for="dynamicmodel-attributename">Attribute Name</label>
<div class="col-md-6">
<input type="text" id="dynamicmodel-attributename" class="form-control" name="DynamicModel[attributeName]">
<div class="help-block help-block-error col-md-3"></div>
<p class="help-block help-block-error col-md-3"></p>
</div>
</div>

11
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('<p class="help-block help-block-error">Some error message.</p>', Html::error($model, 'foo'));
$this->assertEquals('<p class="custom-class">Some error message.</p>', Html::error($model, 'foo', ['class' => 'custom-class']));
$this->assertEquals('<div class="help-block help-block-error">Some error message.</div>', Html::error($model, 'foo', ['tag' => 'div']));
}
}
Loading…
Cancel
Save