Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Changing how 'id' attribute is treated for multiple checkboxes.

Setting the id option, will now also override the generated id's for
checkboxes.
Fixes #1691
  • Loading branch information...
commit 71e2c8e734b496cd47fd7375a0e8fe5ce7f0f95f 1 parent 71f7dcd
@markstory markstory authored
View
47 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
@@ -3949,6 +3949,53 @@ public function testSelectCheckboxMultipleOverrideName() {
}
/**
+ * Test that 'id' overrides all the checkbox id's as well.
+ *
+ * @return void
+ */
+ public function testSelectCheckboxMultipleId() {
+ $result = $this->Form->select(
+ 'Model.multi_field',
+ array('first', 'second', 'third'),
+ array('multiple' => 'checkbox', 'id' => 'CustomId')
+ );
+
+ $expected = array(
+ 'input' => array(
+ 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
+ ),
+ array('div' => array('class' => 'checkbox')),
+ array('input' => array(
+ 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
+ 'value' => '0', 'id' => 'CustomId0'
+ )),
+ array('label' => array('for' => 'CustomId0')),
+ 'first',
+ '/label',
+ '/div',
+ array('div' => array('class' => 'checkbox')),
+ array('input' => array(
+ 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
+ 'value' => '1', 'id' => 'CustomId1'
+ )),
+ array('label' => array('for' => 'CustomId1')),
+ 'second',
+ '/label',
+ '/div',
+ array('div' => array('class' => 'checkbox')),
+ array('input' => array(
+ 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
+ 'value' => '2', 'id' => 'CustomId2'
+ )),
+ array('label' => array('for' => 'CustomId2')),
+ 'third',
+ '/label',
+ '/div'
+ );
+ $this->assertTags($result, $expected);
+ }
+
+/**
* testCheckbox method
*
* Test generation of checkboxes
View
18 lib/Cake/View/Helper/FormHelper.php
@@ -1518,6 +1518,7 @@ public function select($fieldName, $options = array(), $attributes = array()) {
$showParents = $this->_extractOption('showParents', $attributes);
$hiddenField = $this->_extractOption('hiddenField', $attributes);
unset($attributes['escape'], $attributes['secure'], $attributes['empty'], $attributes['showParents'], $attributes['hiddenField']);
+ $id = $this->_extractOption('id', $attributes);
$attributes = $this->_initInputField($fieldName, array_merge(
(array)$attributes, array('secure' => false)
@@ -1573,11 +1574,22 @@ public function select($fieldName, $options = array(), $attributes = array()) {
$options = array_reverse($options, true);
}
+ if (!$id) {
+ $attributes['id'] = Inflector::camelize($attributes['id']);
+ }
+
$select = array_merge($select, $this->__selectOptions(
array_reverse($options, true),
array(),
$showParents,
- array('escape' => $escapeOptions, 'style' => $style, 'name' => $attributes['name'], 'value' => $attributes['value'], 'class' => $attributes['class'])
+ array(
+ 'escape' => $escapeOptions,
+ 'style' => $style,
+ 'name' => $attributes['name'],
+ 'value' => $attributes['value'],
+ 'class' => $attributes['class'],
+ 'id' => $attributes['id']
+ )
));
$template = ($style == 'checkbox') ? 'checkboxmultipleend' : 'selectend';
@@ -2119,9 +2131,7 @@ function __selectOptions($elements = array(), $parents = array(), $showParents =
if ($attributes['style'] === 'checkbox') {
$htmlOptions['value'] = $name;
- $tagName = Inflector::camelize(
- $this->model() . '_' . $this->field().'_'.Inflector::slug($name)
- );
+ $tagName = $attributes['id'] . Inflector::camelize(Inflector::slug($name));
$htmlOptions['id'] = $tagName;
$label = array('for' => $tagName);
Please sign in to comment.
Something went wrong with that request. Please try again.