From f245430eb01688db34081a34d9eadd30eb26fa65 Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Thu, 11 Jan 2018 17:34:05 +0200 Subject: [PATCH] Allow overriding `horizontalCssClasses` when extending `\yii\bootstrap\ActiveField` --- ActiveField.php | 6 ++--- CHANGELOG.md | 1 + tests/ActiveFieldTest.php | 48 ++++++++++++++++++++++++++++++++++++++ tests/data/ExtendedActiveField.php | 23 ++++++++++++++++++ 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 tests/data/ExtendedActiveField.php diff --git a/ActiveField.php b/ActiveField.php index 27f9551..6803711 100644 --- a/ActiveField.php +++ b/ActiveField.php @@ -110,7 +110,7 @@ class ActiveField extends \yii\widgets\ActiveField * - 'error' the error grid class * - 'hint' the hint grid class */ - public $horizontalCssClasses; + public $horizontalCssClasses = []; /** * @var string the template for checkboxes in default layout */ @@ -364,13 +364,13 @@ class ActiveField extends \yii\widgets\ActiveField if ($layout === 'horizontal') { $config['template'] = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}"; - $cssClasses = [ + $cssClasses = array_merge([ 'offset' => 'col-sm-offset-3', 'label' => 'col-sm-3', 'wrapper' => 'col-sm-6', 'error' => '', 'hint' => 'col-sm-3', - ]; + ], $this->horizontalCssClasses); if (isset($instanceConfig['horizontalCssClasses'])) { $cssClasses = ArrayHelper::merge($cssClasses, $instanceConfig['horizontalCssClasses']); } diff --git a/CHANGELOG.md b/CHANGELOG.md index 65ab137..cdfe137 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,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 #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 93c5217..a2b71b0 100644 --- a/tests/ActiveFieldTest.php +++ b/tests/ActiveFieldTest.php @@ -6,6 +6,7 @@ use yii\base\DynamicModel; use yii\bootstrap\ActiveField; use yii\bootstrap\ActiveForm; use Yii; +use yiiunit\extensions\bootstrap\data\ExtendedActiveField; class ActiveFieldTest extends TestCase { @@ -110,4 +111,51 @@ HTML; $this->assertContains('data-attribute="test"', $content); } + + public function testHorizontalCssClasses() + { + $this->helperForm->layout = 'horizontal'; + + $activeField = new ActiveField(['form' => $this->helperForm]); + $activeField->model = $this->helperModel; + $activeField->attribute = $this->attributeName; + + $html = $activeField->render(); + $expectedHtml = << + +
+ +
+
+ + +EXPECTED; + $this->assertEqualsWithoutLE($expectedHtml, $html); + } + + /** + * @depends testHorizontalCssClasses + */ + public function testHorizontalCssClassesOverride() + { + $this->helperForm->layout = 'horizontal'; + + $activeField = new ExtendedActiveField(['form' => $this->helperForm]); + $activeField->model = $this->helperModel; + $activeField->attribute = $this->attributeName; + + $html = $activeField->render(); + $expectedHtml = << + +
+ +
+
+ + +EXPECTED; + $this->assertEqualsWithoutLE($expectedHtml, $html); + } } \ No newline at end of file diff --git a/tests/data/ExtendedActiveField.php b/tests/data/ExtendedActiveField.php new file mode 100644 index 0000000..c4e5c9e --- /dev/null +++ b/tests/data/ExtendedActiveField.php @@ -0,0 +1,23 @@ + + */ +class ExtendedActiveField extends ActiveField +{ + public $horizontalCssClasses = [ + 'offset' => 'col-md-offset-4', + 'label' => 'col-md-4', + 'wrapper' => 'col-md-6', + 'error' => 'col-md-3', + 'hint' => 'col-md-3', + ]; +} \ No newline at end of file