Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markstory committed Oct 31, 2009
1 parent ade96b7 commit 6726b76
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
34 changes: 18 additions & 16 deletions cake/libs/view/helpers/form.php
Expand Up @@ -646,31 +646,33 @@ function inputs($fields = null, $blacklist = null) {
* @return string Completed form widget
*/
function input($fieldName, $options = array()) {
$view =& ClassRegistry::getObject('view');
$this->setEntity($fieldName);

$options = array_merge(
array('before' => null, 'between' => null, 'after' => null),
$this->_inputDefaults,
$options
);

if (!isset($this->fieldset[$this->model()])) {
//Try to load fieldset for this model
$this->_introspectModel($this->model());

$modelKey = $this->model();
$fieldKey = $this->field();
if (!isset($this->fieldset[$modelKey])) {
$this->_introspectModel($modelKey);
}

if (!isset($options['type'])) {
$userType = isset($options['type']) ? true : false;

if (!$userType) {
$options['type'] = 'text';
$fieldDef = array();
if (isset($options['options'])) {
$options['type'] = 'select';
} elseif (in_array($this->field(), array('psword', 'passwd', 'password'))) {
} elseif (in_array($fieldKey, array('psword', 'passwd', 'password'))) {
$options['type'] = 'password';
} elseif (isset($this->fieldset[$this->model()]['fields'][$this->field()])) {
$fieldDef = $this->fieldset[$this->model()]['fields'][$this->field()];
} elseif (isset($this->fieldset[$modelKey]['fields'][$fieldKey])) {
$fieldDef = $this->fieldset[$modelKey]['fields'][$fieldKey];
$type = $fieldDef['type'];
$primaryKey = $this->fieldset[$this->model()]['key'];
$primaryKey = $this->fieldset[$modelKey]['key'];
}

if (isset($type)) {
Expand All @@ -686,12 +688,12 @@ function input($fieldName, $options = array()) {
} elseif (isset($map[$type])) {
$options['type'] = $map[$type];
}
if ($this->field() == $primaryKey) {
if ($fieldKey == $primaryKey) {
$options['type'] = 'hidden';
}
}

if ($this->model() === $this->field()) {
if ($modelKey === $fieldKey) {
$options['type'] = 'select';
if (!isset($options['multiple'])) {
$options['multiple'] = 'multiple';
Expand All @@ -700,10 +702,10 @@ function input($fieldName, $options = array()) {
}
$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');
$varName = Inflector::variable(
Inflector::pluralize(preg_replace('/_id$/', '', $this->field()))
Inflector::pluralize(preg_replace('/_id$/', '', $fieldKey))
);
$varOptions = $view->getVar($varName);
if (is_array($varOptions)) {
Expand Down Expand Up @@ -740,8 +742,8 @@ function input($fieldName, $options = array()) {
$divOptions = array_merge($divOptions, $div);
}
if (
isset($this->fieldset[$this->model()]) &&
in_array($this->field(), $this->fieldset[$this->model()]['validates'])
isset($this->fieldset[$modelKey]) &&
in_array($fieldKey, $this->fieldset[$modelKey]['validates'])
) {
$divOptions = $this->addClass($divOptions, 'required');
}
Expand Down
22 changes: 20 additions & 2 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -1509,14 +1509,14 @@ function testMultipleInputValidation() {
}

/**
* testFormInput method
* testInput method
*
* Test various incarnations of input().
*
* @access public
* @return void
*/
function testFormInput() {
function testInput() {
$result = $this->Form->input('ValidateUser.balance');
$expected = array(
'div' => array('class'),
Expand Down Expand Up @@ -2014,6 +2014,24 @@ function testFormInput() {
}
}

/**
* 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
*
Expand Down

0 comments on commit 6726b76

Please sign in to comment.