Skip to content

Commit

Permalink
Fixing magic select creation for fields that have plural variables ad…
Browse files Browse the repository at this point in the history
…ded to the view.
  • Loading branch information
markstory committed Feb 27, 2010
1 parent 6285f78 commit 3192e13
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cake/libs/view/helpers/form.php
Expand Up @@ -674,6 +674,7 @@ function input($fieldName, $options = array()) {
}

if (!isset($options['type'])) {
$magicType = true;
$options['type'] = 'text';
$fieldDef = array();
if (isset($options['options'])) {
Expand Down Expand Up @@ -716,13 +717,19 @@ function input($fieldName, $options = array()) {
}
$types = array('checkbox', 'radio', 'select');

if (!isset($options['options']) && in_array($options['type'], $types)) {
if (
(!isset($options['options']) && in_array($options['type'], $types)) ||
(isset($magicType) && $options['type'] == 'text')
) {
$view =& ClassRegistry::getObject('view');
$varName = Inflector::variable(
Inflector::pluralize(preg_replace('/_id$/', '', $fieldKey))
);
$varOptions = $view->getVar($varName);
if (is_array($varOptions)) {
if ($options['type'] !== 'radio') {
$options['type'] = 'select';
}
$options['options'] = $varOptions;
}
}
Expand Down
15 changes: 15 additions & 0 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -2061,6 +2061,21 @@ function testInputOverridingMagicSelectType() {
'/div'
);
$this->assertTags($result, $expected);

//Check that magic types still work for plural/singular vars
$view =& ClassRegistry::getObject('view');
$view->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
$result = $this->Form->input('Model.type');
$expected = array(
'div' => array('class' => 'input select'),
'label' => array('for' => 'ModelType'), 'Type', '/label',
'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
array('option' => array('value' => 'value')), 'good', '/option',
array('option' => array('value' => 'other')), 'bad', '/option',
'/select',
'/div'
);
$this->assertTags($result, $expected);
}

/**
Expand Down

0 comments on commit 3192e13

Please sign in to comment.