Skip to content
Permalink
Browse files

Refactoring option extraction into a separate method.

  • Loading branch information...
markstory committed Feb 20, 2010
1 parent 3192e13 commit 1f25bb31c03fbc9ceee0ddc788902e1bf19f1b4e
Showing with 30 additions and 34 deletions.
  1. +30 −34 cake/libs/view/helpers/form.php
@@ -676,7 +676,6 @@ function input($fieldName, $options = array()) {
if (!isset($options['type'])) {
$magicType = true;
$options['type'] = 'text';
$fieldDef = array();
if (isset($options['options'])) {
$options['type'] = 'select';
} elseif (in_array($fieldKey, array('psword', 'passwd', 'password'))) {
@@ -743,13 +742,10 @@ function input($fieldName, $options = array()) {
}
$out = '';
$div = true;
$divOptions = array();
if (array_key_exists('div', $options)) {
$div = $options['div'];
unset($options['div']);
}
$div = $this->_extractOption('div', $options, true);
unset($options['div']);
if (!empty($div)) {
$divOptions['class'] = 'input';
@@ -762,7 +758,7 @@ function input($fieldName, $options = array()) {
if (
isset($this->fieldset[$modelKey]) &&
in_array($fieldKey, $this->fieldset[$modelKey]['validates'])
) {
) {
$divOptions = $this->addClass($divOptions, 'required');
}
if (!isset($divOptions['tag'])) {
@@ -779,11 +775,7 @@ function input($fieldName, $options = array()) {
if ($options['type'] === 'radio') {
$label = false;
if (isset($options['options'])) {
if (is_array($options['options'])) {
$radioOptions = $options['options'];
} else {
$radioOptions = array($options['options']);
}
$radioOptions = (array)$options['options'];
unset($options['options']);
}
}
@@ -817,36 +809,24 @@ function input($fieldName, $options = array()) {
$out = $this->label($fieldName, $labelText, $labelAttributes);
}
$error = null;
if (isset($options['error'])) {
$error = $options['error'];
unset($options['error']);
}
$error = $this->_extractOption('error', $options, null);
unset($options['error']);
$selected = $this->_extractOption('selected', $options, null);
unset($options['selected']);
$selected = null;
if (array_key_exists('selected', $options)) {
$selected = $options['selected'];
unset($options['selected']);
}
if (isset($options['rows']) || isset($options['cols'])) {
$options['type'] = 'textarea';
}
$timeFormat = 12;
if (isset($options['timeFormat'])) {
$timeFormat = $options['timeFormat'];
unset($options['timeFormat']);
}
$dateFormat = 'MDY';
if (isset($options['dateFormat'])) {
$dateFormat = $options['dateFormat'];
unset($options['dateFormat']);
}
if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time' || $options['type'] === 'select') {
$options += array('empty' => false);
}
if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time') {
$dateFormat = $this->_extractOption('dateFormat', $options, 'MDY');
$timeFormat = $this->_extractOption('timeFormat', $options, 12);
unset($options['dateFormat'], $options['timeFormat']);
}
$type = $options['type'];
$before = $options['before'];
@@ -921,6 +901,22 @@ function input($fieldName, $options = array()) {
return $out;
}
/**
* Extracts a single option from an options array.
*
* @param string $name The name of the option to pull out.
* @param array $options The array of options you want to extract.
* @param mixed $default The default option value
* @return the contents of the option or default
* @access protected
*/
function _extractOption($name, $options, $default = null) {
if (array_key_exists($name, $options)) {
return $options[$name];
}
return $default;
}
/**
* Creates a checkbox input widget.
*

0 comments on commit 1f25bb3

Please sign in to comment.
You can’t perform that action at this time.