Skip to content

Commit

Permalink
Trying to make first tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Feb 23, 2014
1 parent ecfd4cf commit 0644544
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 59 deletions.
78 changes: 21 additions & 57 deletions src/View/Helper/FormHelper.php
Expand Up @@ -859,8 +859,7 @@ public function input($fieldName, $options = array()) {
'label' => null,
'error' => null,
'selected' => null,
'options' => null,
'templates' => []
'options' => null
];
$options = $this->_parseOptions($fieldName, $options);

Expand All @@ -870,77 +869,37 @@ public function input($fieldName, $options = array()) {
}

$template = 'groupContainer';
$error = null;
if ($options['type'] !== 'hidden' && $options['error'] !== false) {
$options['error'] = $this->error($fieldName, $error);
$template = 'groupContainerError';
$error = $this->error($fieldName, $options['error']);
$template = empty($error) ? $template : 'groupContainerError';
unset($options['error']);
}

$out['input'] = $this->_getInput($options);
$input = $this->{$options['type']}($fieldName, $options);

return $this->formatTemplate($template, [
'content' => $this->formatTemplate('formGroup', $out),
'error' => $options['error'],
'content' => $this->formatTemplate('formGroup', compact('input', 'label')),
'error' => $error,
'required' => null,
'type' => $options['type'],
]);
}

/**
* Generates an input element
*
* @param array $args The options for the input element
* @return string The generated input element
*/
protected function _getInput($args) {
extract($args);
switch ($type) {
case 'hidden':
return $this->hidden($fieldName, $options);
case 'checkbox':
return $this->checkbox($fieldName, $options);
case 'radio':
return $this->radio($fieldName, $radioOptions, $options);
case 'file':
return $this->file($fieldName, $options);
case 'select':
$options += array('options' => array(), 'value' => $selected);
$list = $options['options'];
unset($options['options']);
return $this->select($fieldName, $list, $options);
case 'time':
$options['value'] = $selected;
return $this->dateTime($fieldName, null, $timeFormat, $options);
case 'date':
$options['value'] = $selected;
return $this->dateTime($fieldName, $dateFormat, null, $options);
case 'datetime':
$options['value'] = $selected;
return $this->dateTime($fieldName, $dateFormat, $timeFormat, $options);
case 'textarea':
return $this->textarea($fieldName, $options + array('cols' => '30', 'rows' => '6'));
case 'url':
return $this->text($fieldName, array('type' => 'url') + $options);
default:
return $this->{$type}($fieldName, $options);
}
}

/**
* Generates input options array
*
* @param array $options
* @return array Options
*/
protected function _parseOptions($fieldName, $options) {
if (!empty($options['type'])) {
$typePassed = true;
if (empty($options['type'])) {
$typePassed = false;
$options['type'] = $this->_inputType($fieldName, $options);
}

$options = $this->_magicOptions($fieldName, $options);

if (in_array(['datetime', 'date', 'time', 'select'], $options['type'])) {
$options += ['empty' => false];
}

$options = $this->_magicOptions($fieldName, $options, $typePassed);
return $options;
}

Expand Down Expand Up @@ -1009,7 +968,7 @@ protected function _optionsOptions($fieldName, $options) {
* @param array $options
* @return array
*/
protected function _magicOptions($fieldName, $options) {
protected function _magicOptions($fieldName, $options, $allowOverride) {
$context = $this->_getContext();
$type = $context->type($fieldName);
$fielDef = $context->attributes($fieldName);
Expand All @@ -1026,7 +985,8 @@ protected function _magicOptions($fieldName, $options) {
// Missing HABTM
//...

if (in_array($options['type'], ['text', 'number', 'radio', 'select'])) {
$typesWithOptions = ['text', 'number', 'radio', 'select'];
if ($allowOverride && in_array($options['type'], $typesWithOptions)) {
$options = $this->_optionsOptions($fieldName, $options);
}

Expand All @@ -1043,6 +1003,10 @@ protected function _magicOptions($fieldName, $options) {
$options['maxlength'] = $fieldDef['length'];
}

if (in_array($options['type'], ['datetime', 'date', 'time', 'select'])) {
$options += ['empty' => false];
}

return $options;
}

Expand Down Expand Up @@ -1096,7 +1060,7 @@ protected function _extractOption($name, $options, $default = null) {
* @return string Generated label element
*/
protected function _inputLabel($fieldName, $label, $options) {
$labelAttributes = $this->domId(array(), 'for');
$labelAttributes = [];
$idKey = null;
if ($options['type'] === 'date' || $options['type'] === 'datetime') {
$firstInput = 'M';
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -2404,8 +2404,8 @@ public function testMultipleInputValidation() {
* @return void
*/
public function testInput() {
$this->markTestIncomplete('Need to revisit once models work again.');
$result = $this->Form->input('ValidateUser.balance');
$this->Form->create('ValidateUsers');

This comment has been minimized.

Copy link
@lorenzo

lorenzo Feb 23, 2014

Author Member

@markstory I'm trying to make input work, but I really cannot figure out how the context should work. No matter what I pass here is somehow wrong. Can you lend me a hand here? This is the 3.0-form-input branch

This comment has been minimized.

Copy link
@markstory

markstory Feb 24, 2014

Member

I can try and fix up some of the tests tonight when I get to my computer.

This comment has been minimized.

Copy link
@lorenzo

lorenzo Feb 24, 2014

Author Member

I already made a bunch of them work, not sure if that was the correct way, though :P

This comment has been minimized.

Copy link
@markstory

markstory Feb 24, 2014

Member

So far I have been passing this->article in as it uses the array context.

$result = $this->Form->input('ValidateUsers.balance');
$expected = array(
'div' => array('class'),
'label' => array('for'),
Expand Down

0 comments on commit 0644544

Please sign in to comment.