Refactoring input() to reduce number of method calls.

Applying patch from 'j3ffy' to allow user defined types to override magic select type.
Test case added.
Fixes #5109
This commit is contained in:
mark_story 2009-10-31 01:45:17 -04:00
parent ade96b7ea5
commit 6726b76077
2 changed files with 38 additions and 18 deletions

View file

@ -646,7 +646,6 @@ class FormHelper extends AppHelper {
* @return string Completed form widget * @return string Completed form widget
*/ */
function input($fieldName, $options = array()) { function input($fieldName, $options = array()) {
$view =& ClassRegistry::getObject('view');
$this->setEntity($fieldName); $this->setEntity($fieldName);
$options = array_merge( $options = array_merge(
@ -654,23 +653,26 @@ class FormHelper extends AppHelper {
$this->_inputDefaults, $this->_inputDefaults,
$options $options
); );
if (!isset($this->fieldset[$this->model()])) { $modelKey = $this->model();
//Try to load fieldset for this model $fieldKey = $this->field();
$this->_introspectModel($this->model()); if (!isset($this->fieldset[$modelKey])) {
$this->_introspectModel($modelKey);
} }
if (!isset($options['type'])) { $userType = isset($options['type']) ? true : false;
if (!$userType) {
$options['type'] = 'text'; $options['type'] = 'text';
$fieldDef = array(); $fieldDef = array();
if (isset($options['options'])) { if (isset($options['options'])) {
$options['type'] = 'select'; $options['type'] = 'select';
} elseif (in_array($this->field(), array('psword', 'passwd', 'password'))) { } elseif (in_array($fieldKey, array('psword', 'passwd', 'password'))) {
$options['type'] = 'password'; $options['type'] = 'password';
} elseif (isset($this->fieldset[$this->model()]['fields'][$this->field()])) { } elseif (isset($this->fieldset[$modelKey]['fields'][$fieldKey])) {
$fieldDef = $this->fieldset[$this->model()]['fields'][$this->field()]; $fieldDef = $this->fieldset[$modelKey]['fields'][$fieldKey];
$type = $fieldDef['type']; $type = $fieldDef['type'];
$primaryKey = $this->fieldset[$this->model()]['key']; $primaryKey = $this->fieldset[$modelKey]['key'];
} }
if (isset($type)) { if (isset($type)) {
@ -686,12 +688,12 @@ class FormHelper extends AppHelper {
} elseif (isset($map[$type])) { } elseif (isset($map[$type])) {
$options['type'] = $map[$type]; $options['type'] = $map[$type];
} }
if ($this->field() == $primaryKey) { if ($fieldKey == $primaryKey) {
$options['type'] = 'hidden'; $options['type'] = 'hidden';
} }
} }
if ($this->model() === $this->field()) { if ($modelKey === $fieldKey) {
$options['type'] = 'select'; $options['type'] = 'select';
if (!isset($options['multiple'])) { if (!isset($options['multiple'])) {
$options['multiple'] = 'multiple'; $options['multiple'] = 'multiple';
@ -700,10 +702,10 @@ class FormHelper extends AppHelper {
} }
$types = array('text', 'checkbox', 'radio', 'select'); $types = array('text', 'checkbox', 'radio', 'select');
if (!isset($options['options']) && in_array($options['type'], $types)) { if (!isset($options['options']) && in_array($options['type'], $types) && !$userType) {
$view =& ClassRegistry::getObject('view'); $view =& ClassRegistry::getObject('view');
$varName = Inflector::variable( $varName = Inflector::variable(
Inflector::pluralize(preg_replace('/_id$/', '', $this->field())) Inflector::pluralize(preg_replace('/_id$/', '', $fieldKey))
); );
$varOptions = $view->getVar($varName); $varOptions = $view->getVar($varName);
if (is_array($varOptions)) { if (is_array($varOptions)) {
@ -740,8 +742,8 @@ class FormHelper extends AppHelper {
$divOptions = array_merge($divOptions, $div); $divOptions = array_merge($divOptions, $div);
} }
if ( if (
isset($this->fieldset[$this->model()]) && isset($this->fieldset[$modelKey]) &&
in_array($this->field(), $this->fieldset[$this->model()]['validates']) in_array($fieldKey, $this->fieldset[$modelKey]['validates'])
) { ) {
$divOptions = $this->addClass($divOptions, 'required'); $divOptions = $this->addClass($divOptions, 'required');
} }

View file

@ -1509,14 +1509,14 @@ class FormHelperTest extends CakeTestCase {
} }
/** /**
* testFormInput method * testInput method
* *
* Test various incarnations of input(). * Test various incarnations of input().
* *
* @access public * @access public
* @return void * @return void
*/ */
function testFormInput() { function testInput() {
$result = $this->Form->input('ValidateUser.balance'); $result = $this->Form->input('ValidateUser.balance');
$expected = array( $expected = array(
'div' => array('class'), 'div' => array('class'),
@ -2014,6 +2014,24 @@ class FormHelperTest extends CakeTestCase {
} }
} }
/**
* test that overriding the magic select type widget is possible
*
* @return void
**/
function testInputOverridingMagicSelectType() {
$view =& ClassRegistry::getObject('view');
$view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
$result = $this->Form->input('Model.user_id', array('type' => 'text'));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'ModelUserId'), 'User', '/label',
'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId', 'value' => ''),
'/div'
);
$this->assertTags($result, $expected);
}
/** /**
* testFormInputs method * testFormInputs method
* *