Permalink
Browse files

Implement id prefixing for radio and multicheckbox widgets.

  • Loading branch information...
1 parent 78c8b3e commit 3084a020d2546a280615b92253ca45d4347188b8 @ADmad ADmad committed Mar 9, 2014
@@ -1197,6 +1197,7 @@ public function radio($fieldName, $options = [], $attributes = []) {
]);
}
$attributes['options'] = $options;
+ $attributes['idPrefix'] = $this->_idPrefix;
return $hidden . $this->widget('radio', $attributes);
}
@@ -1653,6 +1654,7 @@ public function multiCheckbox($fieldName, $options, $attributes = []) {
];
$attributes = $this->_initInputField($fieldName, $attributes);
$attributes['options'] = $options;
+ $attributes['idPrefix'] = $this->_idPrefix;
$hidden = '';
if ($attributes['hiddenField']) {
@@ -170,7 +170,7 @@ public function formatAttributes($options, $exclude = null) {
$exclude = [];
}
- $exclude = ['escape' => true] + array_flip($exclude);
+ $exclude = ['escape' => true, 'idPrefix' => true] + array_flip($exclude);
$escape = $options['escape'];
$attributes = [];
@@ -71,6 +71,7 @@ public function __construct($templates, $label) {
* - `disabled` Either a boolean or an array of checkboxes to disable.
* - `escape` Set to false to disable HTML escaping.
* - `options` An associative array of value=>labels to generate options for.
+ * - `idPrefix` Prefix for generated ID attributes.
*
* ### Options format
*
@@ -105,8 +106,10 @@ public function render(array $data) {
'options' => [],
'disabled' => null,
'val' => null,
+ 'idPrefix' => null
];
$out = [];
+ $this->_idPrefix = $data['idPrefix'];
$this->_clearIds();
foreach ($data['options'] as $key => $val) {
$checkbox = [
@@ -75,6 +75,7 @@ public function __construct($templates, $label) {
* an array of attributes for all labels.
* - `required` - Set to true to add the required attribute
* on all generated radios.
+ * - `idPrefix` Prefix for generated ID attributes.
*
* @param array $data The data to build radio buttons with.
* @return string
@@ -88,6 +89,7 @@ public function render(array $data) {
'escape' => true,
'label' => true,
'empty' => false,
+ 'idPrefix' => null
];
if ($data['options'] instanceof Traversable) {
$options = iterator_to_array($data['options']);
@@ -101,6 +103,7 @@ public function render(array $data) {
}
unset($data['empty']);
+ $this->_idPrefix = $data['idPrefix'];
$this->_clearIds();
$opts = [];
foreach ($options as $val => $text) {
@@ -2019,40 +2019,91 @@ public function testInputCustomization() {
* @return void
*/
public function testCreateIdPrefix() {
- $this->Form->create(false, array('idPrefix' => 'prefix'));
+ $this->Form->create(false, ['idPrefix' => 'prefix']);
$result = $this->Form->input('field');
- $expected = array(
- 'div' => array('class' => 'input text'),
- 'label' => array('for' => 'prefix-field'),
+ $expected = [
+ 'div' => ['class' => 'input text'],
+ 'label' => ['for' => 'prefix-field'],
'Field',
'/label',
- 'input' => array('type' => 'text', 'name' => 'field', 'id' => 'prefix-field'),
+ 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
'/div'
- );
+ ];
$this->assertTags($result, $expected);
$result = $this->Form->input('field', ['id' => 'custom-id']);
- $expected = array(
- 'div' => array('class' => 'input text'),
- 'label' => array('for' => 'custom-id'),
+ $expected = [
+ 'div' => ['class' => 'input text'],
+ 'label' => ['for' => 'custom-id'],
'Field',
'/label',
- 'input' => array('type' => 'text', 'name' => 'field', 'id' => 'custom-id'),
+ 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
'/div'
+ ];
+ $this->assertTags($result, $expected);
+
+ $result = $this->Form->radio('Model.field', ['option A']);
+ $expected = [
+ 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
+ ['input' => [
+ 'type' => 'radio',
+ 'name' => 'Model[field]',
+ 'value' => '0',
+ 'id' => 'prefix-model-field-0'
+ ]],
+ 'label' => ['for' => 'prefix-model-field-0'],
+ 'option A',
+ '/label'
+ ];
+ $this->assertTags($result, $expected);
+
+ $result = $this->Form->radio('Model.field', ['option A', 'option']);
+ $expected = [
+ 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
+ ['input' => [
+ 'type' => 'radio',
+ 'name' => 'Model[field]',
+ 'value' => '0',
+ 'id' => 'prefix-model-field-0'
+ ]],
+ 'label' => ['for' => 'prefix-model-field-0'],
+ 'option A',
+ '/label'
+ ];
+ $this->assertTags($result, $expected);
+
+ $result = $this->Form->select(
+ 'Model.multi_field',
+ ['first'],
+ ['multiple' => 'checkbox']
);
+ $expected = [
+ 'input' => [
+ 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
+ ],
+ ['div' => ['class' => 'checkbox']],
+ ['input' => [
+ 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
+ 'value' => '0', 'id' => 'prefix-model-multi-field-0'
+ ]],
+ ['label' => ['for' => 'prefix-model-multi-field-0']],
+ 'first',
+ '/label',
+ '/div',
+ ];
$this->assertTags($result, $expected);
$this->Form->end();
$result = $this->Form->input('field');
- $expected = array(
- 'div' => array('class' => 'input text'),
- 'label' => array('for' => 'field'),
+ $expected = [
+ 'div' => ['class' => 'input text'],
+ 'label' => ['for' => 'field'],
'Field',
'/label',
- 'input' => array('type' => 'text', 'name' => 'field', 'id' => 'field'),
+ 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
'/div'
- );
+ ];
$this->assertTags($result, $expected);
}
@@ -2172,6 +2223,48 @@ public function testInputDatetime() {
}
/**
+ * test form->input() with datetime with id prefix
+ *
+ * @return void
+ */
+ public function testInputDatetimeIdPrefix() {
+ $this->Form = $this->getMock(
+ 'Cake\View\Helper\FormHelper',
+ ['datetime'],
+ [new View(null)]
+ );
+
+ $this->Form->create(false, ['idPrefix' => 'prefix']);
+
+ $this->Form->expects($this->once())->method('datetime')
+ ->with('prueba', [
+ 'type' => 'datetime',
+ 'timeFormat' => 24,
+ 'minYear' => 2008,
+ 'maxYear' => 2011,
+ 'interval' => 15,
+ 'options' => null,
+ 'empty' => false,
+ 'id' => 'prefix-prueba',
+ 'required' => false,
+ ])
+ ->will($this->returnValue('This is it!'));
+ $result = $this->Form->input('prueba', array(
+ 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
+ 'maxYear' => 2011, 'interval' => 15
+ ));
+ $expected = array(
+ 'div' => array('class' => 'input datetime'),
+ 'label' => array('for' => 'prefix-prueba'),
+ 'Prueba',
+ '/label',
+ 'This is it!',
+ '/div'
+ );
+ $this->assertTags($result, $expected);
+ }
+
+/**
* Test generating checkboxes with disabled elements.
*
* @return void

0 comments on commit 3084a02

Please sign in to comment.