diff --git a/CHANGELOG.md b/CHANGELOG.md index ef8d6e8..333db71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 bootstrap extension Change Log 2.0.5 under development ----------------------- - +- Enh #38: Added support to object for content option in Collapse class (pana1990, ItsReddi) 2.0.4 May 10, 2015 ------------------ diff --git a/Collapse.php b/Collapse.php index a2f237a..c45e966 100644 --- a/Collapse.php +++ b/Collapse.php @@ -61,7 +61,7 @@ class Collapse extends Widget * - label: string, required, the group header label. * - encode: boolean, optional, whether this label should be HTML-encoded. This param will override * global `$this->encodeLabels` param. - * - content: array|string, required, the content (HTML) of the group + * - content: array|string|object, required, the content (HTML) of the group * - options: array, optional, the HTML attributes of the group * - contentOptions: optional, the HTML attributes of the group's content */ @@ -146,7 +146,7 @@ class Collapse extends Widget $header = Html::tag('h4', $headerToggle, ['class' => 'panel-title']); - if (is_string($item['content'])) { + if (is_string($item['content']) || is_object($item['content'])) { $content = Html::tag('div', $item['content'], ['class' => 'panel-body']) . "\n"; } elseif (is_array($item['content'])) { $content = Html::ul($item['content'], [ @@ -160,7 +160,7 @@ class Collapse extends Widget $content .= Html::tag('div', $item['footer'], ['class' => 'panel-footer']) . "\n"; } } else { - throw new InvalidConfigException('The "content" option should be a string or array.'); + throw new InvalidConfigException('The "content" option should be a string, array or object.'); } } else { throw new InvalidConfigException('The "content" option is required.'); diff --git a/tests/CollapseTest.php b/tests/CollapseTest.php index 8c294b3..aa703bc 100644 --- a/tests/CollapseTest.php +++ b/tests/CollapseTest.php @@ -108,4 +108,39 @@ class CollapseTest extends TestCase HTML , $output); } + + /** + * @see https://github.com/yiisoft/yii2/issues/8357 + */ + public function testRenderObject() + { + $template = ['template' => '{input}']; + ob_start(); + $form = \yii\widgets\ActiveForm::begin(['action' => '/something']); + ob_end_clean(); + $model = new data\Singer; + + Collapse::$counter = 0; + $output = Collapse::widget([ + 'items' => [ + [ + 'label' => 'Collapsible Group Item #1', + 'content' => $form->field($model, 'firstName', $template) + ], + ] + ]); + + $this->assertEqualsWithoutLE(<< +

Collapsible Group Item #1 +

+
+ +
+
+ + +HTML + , $output); + } } diff --git a/tests/data/Singer.php b/tests/data/Singer.php new file mode 100644 index 0000000..bd8e15e --- /dev/null +++ b/tests/data/Singer.php @@ -0,0 +1,27 @@ + + */ +class Singer extends Model +{ + public $firstName; + public $lastName; + public $test; + + public function rules() + { + return [ + [['lastName'], 'default', 'value' => 'Lennon'], + [['lastName'], 'required'], + [['underscore_style'], 'yii\captcha\CaptchaValidator'], + [['test'], 'required', 'when' => function($model) { return $model->firstName === 'cebe'; }], + ]; + } +}