Browse Source

Fix 29: Added support to object for content option in Collapse class

tags/2.0.6
pana1990 10 years ago
parent
commit
f7cc408d88
  1. 2
      CHANGELOG.md
  2. 6
      Collapse.php
  3. 35
      tests/CollapseTest.php
  4. 27
      tests/data/Singer.php

2
CHANGELOG.md

@ -4,7 +4,7 @@ Yii Framework 2 bootstrap extension Change Log
2.0.5 under development 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 2.0.4 May 10, 2015
------------------ ------------------

6
Collapse.php

@ -61,7 +61,7 @@ class Collapse extends Widget
* - label: string, required, the group header label. * - label: string, required, the group header label.
* - encode: boolean, optional, whether this label should be HTML-encoded. This param will override * - encode: boolean, optional, whether this label should be HTML-encoded. This param will override
* global `$this->encodeLabels` param. * 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 * - options: array, optional, the HTML attributes of the group
* - contentOptions: optional, the HTML attributes of the group's content * - 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']); $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"; $content = Html::tag('div', $item['content'], ['class' => 'panel-body']) . "\n";
} elseif (is_array($item['content'])) { } elseif (is_array($item['content'])) {
$content = Html::ul($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"; $content .= Html::tag('div', $item['footer'], ['class' => 'panel-footer']) . "\n";
} }
} else { } 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 { } else {
throw new InvalidConfigException('The "content" option is required.'); throw new InvalidConfigException('The "content" option is required.');

35
tests/CollapseTest.php

@ -108,4 +108,39 @@ class CollapseTest extends TestCase
HTML HTML
, $output); , $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(<<<HTML
<div id="w0" class="panel-group">
<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a class="collapse-toggle" href="#w0-collapse1" data-toggle="collapse" data-parent="#w0">Collapsible Group Item #1</a>
</h4></div>
<div id="w0-collapse1" class="panel-collapse collapse"><div class="panel-body"><div class="form-group field-singer-firstname">
<input type="text" id="singer-firstname" class="form-control" name="Singer[firstName]">
</div></div>
</div></div>
</div>
HTML
, $output);
}
} }

27
tests/data/Singer.php

@ -0,0 +1,27 @@
<?php
namespace yiiunit\extensions\bootstrap\data;
use yii\base\Model;
/**
* Class Singer
*
* @author Daniel Gomez Pan <pana_1990@hotmail.com>
*/
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'; }],
];
}
}
Loading…
Cancel
Save