Permalink
Browse files

Fixing issues with magic input() type detection and adding/updating t…

…ests.
  • Loading branch information...
1 parent d8e7aa9 commit 84840c7b04a30b0fab4651f99e3d38d8aa313180 @markstory markstory committed Jan 13, 2010
Showing with 22 additions and 13 deletions.
  1. +6 −8 cake/libs/view/helpers/form.php
  2. +16 −5 cake/tests/cases/libs/view/helpers/form.test.php
@@ -661,9 +661,7 @@ function input($fieldName, $options = array()) {
$this->_introspectModel($modelKey);
}
- $userType = isset($options['type']) ? true : false;
-
- if (!$userType) {
+ if (!isset($options['type'])) {
$options['type'] = 'text';
$fieldDef = array();
if (isset($options['options'])) {
@@ -693,6 +691,9 @@ function input($fieldName, $options = array()) {
$options['type'] = 'hidden';
}
}
+ if (preg_match('/_id$/', $fieldKey)) {
+ $options['type'] = 'select';
+ }
if ($modelKey === $fieldKey) {
$options['type'] = 'select';
@@ -701,18 +702,15 @@ function input($fieldName, $options = array()) {
}
}
}
- $types = array('text', 'checkbox', 'radio', 'select');
+ $types = array('checkbox', 'radio', 'select');
- if (!isset($options['options']) && in_array($options['type'], $types) && !$userType) {
+ if (!isset($options['options']) && in_array($options['type'], $types)) {
$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;
}
}
@@ -2050,6 +2050,18 @@ function testInputOverridingMagicSelectType() {
}
/**
+ * Test that magic input() selects can easily be converted into radio types without error.
+ *
+ * @return void
+ */
+ function testInputMagicSelectChangeToRadio() {
+ $view =& ClassRegistry::getObject('view');
+ $view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
+ $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
+ $this->assertPattern('/input type="radio"/', $result);
+ }
+
+/**
* testFormInputs method
*
* test correct results from form::inputs().
@@ -5930,17 +5942,16 @@ function testMultiRecordForm() {
$this->Form->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
$result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
$expected = array(
- 'div' => array('class' => 'input text error'),
+ 'div' => array('class' => 'input select error'),
'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
'Profile',
'/label',
- 'input' => array(
- 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]', 'type' => 'text',
- 'value' => '1',
+ 'select' => array(
+ 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
'id' => 'ValidateProfile1ValidateItem2ProfileId',
- 'maxlength' => 8,
'class' => 'form-error'
),
+ '/select',
array('div' => array('class' => 'error-message')),
'Error',
'/div',

0 comments on commit 84840c7

Please sign in to comment.