Skip to content

Commit 3192e13

Browse files
committed
Fixing magic select creation for fields that have plural variables added to the view.
1 parent 6285f78 commit 3192e13

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

cake/libs/view/helpers/form.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ function input($fieldName, $options = array()) {
674674
}
675675

676676
if (!isset($options['type'])) {
677+
$magicType = true;
677678
$options['type'] = 'text';
678679
$fieldDef = array();
679680
if (isset($options['options'])) {
@@ -716,13 +717,19 @@ function input($fieldName, $options = array()) {
716717
}
717718
$types = array('checkbox', 'radio', 'select');
718719

719-
if (!isset($options['options']) && in_array($options['type'], $types)) {
720+
if (
721+
(!isset($options['options']) && in_array($options['type'], $types)) ||
722+
(isset($magicType) && $options['type'] == 'text')
723+
) {
720724
$view =& ClassRegistry::getObject('view');
721725
$varName = Inflector::variable(
722726
Inflector::pluralize(preg_replace('/_id$/', '', $fieldKey))
723727
);
724728
$varOptions = $view->getVar($varName);
725729
if (is_array($varOptions)) {
730+
if ($options['type'] !== 'radio') {
731+
$options['type'] = 'select';
732+
}
726733
$options['options'] = $varOptions;
727734
}
728735
}

cake/tests/cases/libs/view/helpers/form.test.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,21 @@ function testInputOverridingMagicSelectType() {
20612061
'/div'
20622062
);
20632063
$this->assertTags($result, $expected);
2064+
2065+
//Check that magic types still work for plural/singular vars
2066+
$view =& ClassRegistry::getObject('view');
2067+
$view->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
2068+
$result = $this->Form->input('Model.type');
2069+
$expected = array(
2070+
'div' => array('class' => 'input select'),
2071+
'label' => array('for' => 'ModelType'), 'Type', '/label',
2072+
'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
2073+
array('option' => array('value' => 'value')), 'good', '/option',
2074+
array('option' => array('value' => 'other')), 'bad', '/option',
2075+
'/select',
2076+
'/div'
2077+
);
2078+
$this->assertTags($result, $expected);
20642079
}
20652080

20662081
/**

0 commit comments

Comments
 (0)