Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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...
commit 6726b76077f17d02eb018f31b9ef88fa65c8bde3 1 parent ade96b7
@markstory markstory authored
View
34 cake/libs/view/helpers/form.php
@@ -646,7 +646,6 @@ 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(
@@ -654,23 +653,26 @@ function input($fieldName, $options = array()) {
$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)) {
@@ -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';
@@ -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)) {
@@ -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');
}
View
22 cake/tests/cases/libs/view/helpers/form.test.php
@@ -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'),
@@ -2015,6 +2015,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
*
* test correct results from form::inputs().
Please sign in to comment.
Something went wrong with that request. Please try again.