Permalink
Browse files

Starting to implement FormHelper::input()

  • Loading branch information...
1 parent b7cf739 commit 8c536d105c179f87c0fb1b4144a8db4d0593e1bd @lorenzo lorenzo committed Feb 23, 2014
Showing with 26 additions and 118 deletions.
  1. +26 −118 src/View/Helper/FormHelper.php
@@ -158,6 +158,9 @@ class FormHelper extends Helper {
'radio' => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>',
'radioContainer' => '{{input}}{{label}}',
'textarea' => '<textarea name="{{name}}"{{attrs}}>{{value}}</textarea>',
+ 'formGroup' => '{{label}}{{input}}',
+ 'groupContainer' => '<div class="{{type}}{{required}}">{{content}}</div>',
+ 'groupContainerError' => '<div class="{{type}}{{required}} error">{{content}}{{error}}</div>'
];
/**
@@ -845,71 +848,34 @@ public function inputs($fields = null, $blacklist = null, $options = array()) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#creating-form-elements
*/
public function input($fieldName, $options = array()) {
- $this->setEntity($fieldName);
+ $options += [
+ 'type' => null,
+ 'label' => null,
+ 'error' => null,
+ 'selected' => null,
+ 'options' => null,
+ 'templates' => []
+ ];
$options = $this->_parseOptions($options);
- $divOptions = $this->_divOptions($options);
- unset($options['div']);
-
- if ($options['type'] === 'radio' && isset($options['options'])) {
- $radioOptions = (array)$options['options'];
- unset($options['options']);
- }
-
$label = $this->_getLabel($fieldName, $options);
if ($options['type'] !== 'radio') {
unset($options['label']);
}
- $error = $this->_extractOption('error', $options, null);
- unset($options['error']);
-
- $errorMessage = $this->_extractOption('errorMessage', $options, true);
- unset($options['errorMessage']);
-
- $selected = $this->_extractOption('selected', $options, null);
- unset($options['selected']);
-
- 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']);
+ $template = 'groupContainer';
+ if ($options['type'] !== 'hidden' && $options['error'] !== false) {
+ $options['error'] = $this->error($fieldName, $error);
+ $template = 'groupContainerError';
}
- $type = $options['type'];
- $out = array('before' => $options['before'], 'label' => $label, 'between' => $options['between'], 'after' => $options['after']);
- $format = $this->_getFormat($options);
-
- unset($options['type'], $options['before'], $options['between'], $options['after'], $options['format']);
+ $out['input'] = $this->_getInput($options);
- $out['error'] = null;
- if ($type !== 'hidden' && $error !== false) {
- $errMsg = $this->error($fieldName, $error);
- if ($errMsg) {
- $divOptions = $this->addClass($divOptions, 'error');
- if ($errorMessage) {
- $out['error'] = $errMsg;
- }
- }
- }
-
- if ($type === 'radio' && isset($out['between'])) {
- $options['between'] = $out['between'];
- $out['between'] = null;
- }
- $out['input'] = $this->_getInput(compact('type', 'fieldName', 'options', 'radioOptions', 'selected', 'dateFormat', 'timeFormat'));
-
- $output = '';
- foreach ($format as $element) {
- $output .= $out[$element];
- }
-
- if (!empty($divOptions['tag'])) {
- $tag = $divOptions['tag'];
- unset($divOptions['tag']);
- $output = $this->Html->tag($tag, $output, $divOptions);
- }
- return $output;
+ return $this->formatTemplate($template, [
+ 'content' => $this->formatTemplate('formGroup', $out),
+ 'error' => $options['error'],
+ 'type' => $options['type'],
+ ]);
}
/**
@@ -959,27 +925,22 @@ protected function _getInput($args) {
* @return array Options
*/
protected function _parseOptions($options) {
- $options = array_merge(
- array('before' => null, 'between' => null, 'after' => null, 'format' => null),
- $this->_inputDefaults,
- $options
- );
-
- if (!isset($options['type'])) {
+ if (!empty($options['type'])) {
$options = $this->_magicOptions($options);
}
- if (in_array($options['type'], array('radio', 'select'))) {
+ if (in_array($options['type'], ['radio', 'select'])) {
$options = $this->_optionsOptions($options);
}
if (isset($options['rows']) || isset($options['cols'])) {
$options['type'] = 'textarea';
}
- if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time' || $options['type'] === 'select') {
- $options += array('empty' => false);
+ if (in_array(['datetime', 'date', 'time', 'select'], $options['type'])) {
+ $options += ['empty' => false];
}
+
return $options;
}
@@ -1081,25 +1042,6 @@ protected function _magicOptions($options) {
}
/**
- * Generate format options
- *
- * @param array $options
- * @return array
- */
- protected function _getFormat($options) {
- if ($options['type'] === 'hidden') {
- return array('input');
- }
- if (is_array($options['format']) && in_array('input', $options['format'])) {
- return $options['format'];
- }
- if ($options['type'] === 'checkbox') {
- return array('before', 'input', 'between', 'label', 'after', 'error');
- }
- return array('before', 'label', 'between', 'input', 'after', 'error');
- }
-
-/**
* Generate label for input
*
* @param string $fieldName
@@ -1145,40 +1087,6 @@ protected function _maxLength($options) {
}
/**
- * Generate div options for input
- *
- * @param array $options
- * @return array
- */
- protected function _divOptions($options) {
- if ($options['type'] === 'hidden') {
- return array();
- }
- $div = $this->_extractOption('div', $options, true);
- if (!$div) {
- return array();
- }
-
- $divOptions = array('class' => 'input');
- $divOptions = $this->addClass($divOptions, $options['type']);
- if (is_string($div)) {
- $divOptions['class'] = $div;
- } elseif (is_array($div)) {
- $divOptions = array_merge($divOptions, $div);
- }
- if (
- $this->_extractOption('required', $options) !== false &&
- $this->_introspectModel($this->model(), 'validates', $this->field())
- ) {
- $divOptions = $this->addClass($divOptions, 'required');
- }
- if (!isset($divOptions['tag'])) {
- $divOptions['tag'] = 'div';
- }
- return $divOptions;
- }
-
-/**
* Extracts a single option from an options array.
*
* @param string $name The name of the option to pull out.

0 comments on commit 8c536d1

Please sign in to comment.