Browse Source

Allow overriding `horizontalCssClasses` when extending `\yii\bootstrap\ActiveField`

tags/2.0.8
Klimov Paul 7 years ago
parent
commit
f245430eb0
  1. 6
      ActiveField.php
  2. 1
      CHANGELOG.md
  3. 48
      tests/ActiveFieldTest.php
  4. 23
      tests/data/ExtendedActiveField.php

6
ActiveField.php

@ -110,7 +110,7 @@ class ActiveField extends \yii\widgets\ActiveField
* - 'error' the error grid class * - 'error' the error grid class
* - 'hint' the hint grid class * - 'hint' the hint grid class
*/ */
public $horizontalCssClasses; public $horizontalCssClasses = [];
/** /**
* @var string the template for checkboxes in default layout * @var string the template for checkboxes in default layout
*/ */
@ -364,13 +364,13 @@ class ActiveField extends \yii\widgets\ActiveField
if ($layout === 'horizontal') { if ($layout === 'horizontal') {
$config['template'] = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}"; $config['template'] = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}";
$cssClasses = [ $cssClasses = array_merge([
'offset' => 'col-sm-offset-3', 'offset' => 'col-sm-offset-3',
'label' => 'col-sm-3', 'label' => 'col-sm-3',
'wrapper' => 'col-sm-6', 'wrapper' => 'col-sm-6',
'error' => '', 'error' => '',
'hint' => 'col-sm-3', 'hint' => 'col-sm-3',
]; ], $this->horizontalCssClasses);
if (isset($instanceConfig['horizontalCssClasses'])) { if (isset($instanceConfig['horizontalCssClasses'])) {
$cssClasses = ArrayHelper::merge($cssClasses, $instanceConfig['horizontalCssClasses']); $cssClasses = ArrayHelper::merge($cssClasses, $instanceConfig['horizontalCssClasses']);
} }

1
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 #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 #219: Add ability to use custom HTML in navbar-header (razvanphp)
- Enh #171: Add ability to use a brandImage with the navbar (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) - Enh #227: Added `yii\bootstrap\Collapse::$itemToggleOptions` allowing setup custom collapse tag name and HTML options (mskayali, klimov-paul)

48
tests/ActiveFieldTest.php

@ -6,6 +6,7 @@ use yii\base\DynamicModel;
use yii\bootstrap\ActiveField; use yii\bootstrap\ActiveField;
use yii\bootstrap\ActiveForm; use yii\bootstrap\ActiveForm;
use Yii; use Yii;
use yiiunit\extensions\bootstrap\data\ExtendedActiveField;
class ActiveFieldTest extends TestCase class ActiveFieldTest extends TestCase
{ {
@ -110,4 +111,51 @@ HTML;
$this->assertContains('data-attribute="test"', $content); $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
<div class="form-group field-dynamicmodel-attributename">
<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>
</div>
</div>
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
<div class="form-group field-dynamicmodel-attributename">
<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>
</div>
</div>
EXPECTED;
$this->assertEqualsWithoutLE($expectedHtml, $html);
}
} }

23
tests/data/ExtendedActiveField.php

@ -0,0 +1,23 @@
<?php
namespace yiiunit\extensions\bootstrap\data;
use yii\bootstrap\ActiveField;
/**
* A customized extension from ActiveField
*
* @see \yiiunit\extensions\bootstrap\ActiveFieldTest::testHorizontalCssClassesOverride()
*
* @author Michael Härtl <haertl.mike@gmail.com>
*/
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',
];
}
Loading…
Cancel
Save