Skip to content

Commit

Permalink
Updating FormHelper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 14, 2011
1 parent e8efc67 commit 1941be6
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 103 deletions.
68 changes: 34 additions & 34 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -1574,7 +1574,7 @@ public function testFormValidationMultiRecord() {
* @return void
*/
public function testMultipleInputValidation() {
$Address = ClassRegistry::getObject('Address');
$Address = ClassRegistry::init(array('class' => 'Address', 'table' => false));
$Address->validationErrors[0] = array(
'title' => array('This field cannot be empty'),
'first_name' => array('This field cannot be empty')
Expand Down Expand Up @@ -1789,16 +1789,17 @@ public function testInput() {

unset($this->Form->request->data);

$this->Form->validationErrors['Model']['field'] = array('Badness!');
$result = $this->Form->input('Model.field');
$Contact = ClassRegistry::getObject('Contact');
$Contact->validationErrors['field'] = array('Badness!');
$result = $this->Form->input('Contact.field');
$expected = array(
'div' => array('class' => 'input text error'),
'label' => array('for' => 'ModelField'),
'label' => array('for' => 'ContactField'),
'Field',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Model][field]',
'id' => 'ModelField', 'class' => 'form-error'
'type' => 'text', 'name' => 'data[Contact][field]',
'id' => 'ContactField', 'class' => 'form-error'
),
array('div' => array('class' => 'error-message')),
'Badness!',
Expand All @@ -1807,49 +1808,49 @@ public function testInput() {
);
$this->assertTags($result, $expected);

$result = $this->Form->input('Model.field', array(
$result = $this->Form->input('Contact.field', array(
'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
));
$expected = array(
'label' => array('for' => 'ModelField'),
'label' => array('for' => 'ContactField'),
'Field',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Model][field]',
'id' => 'ModelField', 'class' => 'form-error'
'type' => 'text', 'name' => 'data[Contact][field]',
'id' => 'ContactField', 'class' => 'form-error'
),
array('span' => array('class' => 'error-message')),
'Badness!',
'/span'
);
$this->assertTags($result, $expected);

$result = $this->Form->input('Model.field', array(
$result = $this->Form->input('Contact.field', array(
'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
));
$expected = array(
'span' => array('class' => 'input text error'),
'label' => array('for' => 'ModelField'),
'label' => array('for' => 'ContactField'),
'Field',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Model][field]',
'id' => 'ModelField', 'class' => 'form-error'
'type' => 'text', 'name' => 'data[Contact][field]',
'id' => 'ContactField', 'class' => 'form-error'
),
'Badness!',
'/span'
);
$this->assertTags($result, $expected);

$result = $this->Form->input('Model.field', array('after' => 'A message to you, Rudy'));
$result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
$expected = array(
'div' => array('class' => 'input text error'),
'label' => array('for' => 'ModelField'),
'label' => array('for' => 'ContactField'),
'Field',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Model][field]',
'id' => 'ModelField', 'class' => 'form-error'
'type' => 'text', 'name' => 'data[Contact][field]',
'id' => 'ContactField', 'class' => 'form-error'
),
'A message to you, Rudy',
array('div' => array('class' => 'error-message')),
Expand All @@ -1860,56 +1861,55 @@ public function testInput() {
$this->assertTags($result, $expected);

$this->Form->setEntity(null);
$this->Form->setEntity('Model.field');
$result = $this->Form->input('Model.field', array(
$this->Form->setEntity('Contact.field');
$result = $this->Form->input('Contact.field', array(
'after' => 'A message to you, Rudy', 'error' => false
));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'ModelField'),
'label' => array('for' => 'ContactField'),
'Field',
'/label',
'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField', 'class' => 'form-error'),
'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
'A message to you, Rudy',
'/div'
);
$this->assertTags($result, $expected);

unset($this->Form->validationErrors['Model']['field']);
$result = $this->Form->input('Model.field', array('after' => 'A message to you, Rudy'));
$result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'ModelField'),
'label' => array('for' => 'ObjectField'),
'Field',
'/label',
'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField'),
'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
'A message to you, Rudy',
'/div'
);
$this->assertTags($result, $expected);

$this->Form->validationErrors['Model']['field'] = array('minLength');
$result = $this->Form->input('Model.field', array(
$Contact->validationErrors['field'] = array('minLength');
$result = $this->Form->input('Contact.field', array(
'error' => array(
'minLength' => 'Le login doit contenir au moins 2 caractères',
'maxLength' => 'login too large'
)
));
$expected = array(
'div' => array('class' => 'input text error'),
'label' => array('for' => 'ModelField'),
'label' => array('for' => 'ContactField'),
'Field',
'/label',
'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField', 'class' => 'form-error'),
'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
array('div' => array('class' => 'error-message')),
'Le login doit contenir au moins 2 caractères',
'/div',
'/div'
);
$this->assertTags($result, $expected);

$this->Form->validationErrors['Model']['field'] = array('maxLength');
$result = $this->Form->input('Model.field', array(
$Contact->validationErrors['field'] = array('maxLength');
$result = $this->Form->input('Contact.field', array(
'error' => array(
'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
'minLength' => 'Le login doit contenir au moins 2 caractères',
Expand All @@ -1918,10 +1918,10 @@ public function testInput() {
));
$expected = array(
'div' => array('class' => 'input text error'),
'label' => array('for' => 'ModelField'),
'label' => array('for' => 'ContactField'),
'Field',
'/label',
'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField', 'class' => 'form-error'),
'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
array('span' => array('class' => 'error-message', 'rel' => 'fake')),
'login too large',
'/span',
Expand Down
136 changes: 67 additions & 69 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -134,10 +134,10 @@ protected function _getModel($model) {
'alias' => $model
));
} else {
$object = ClassRegistry::init($model);
$object = ClassRegistry::init($model, true);
}

if (get_class($object) === 'AppModel') {
if (!$object) {
return null;
}

Expand All @@ -153,27 +153,27 @@ protected function _introspectModel($model, $key, $field = null) {
}

if ($key === 'key') {
return $this->fieldset[$object->alias]['key'];
return $this->fieldset[$model]['key'];
}

if (!isset($this->fieldset[$object->alias]['fields'])) {
$fields = $this->fieldset[$object->alias]['fields'] = $object->schema();
if (!isset($this->fieldset[$model]['fields'])) {
$fields = $this->fieldset[$model]['fields'] = $object->schema();
}

if ($key === 'fields') {
if (empty($field)) {
foreach ($object->hasAndBelongsToMany as $alias => $assocData) {
$this->fieldset[$object->alias]['fields'][$alias] = array('type' => 'multiple');
}
return $this->fieldset[$object->alias]['fields'];
} elseif (isset($this->fieldset[$object->alias]['fields'][$field])) {
return $this->fieldset[$object->alias]['fields'][$field];
return $this->fieldset[$model]['fields'];
} elseif (isset($this->fieldset[$model]['fields'][$field])) {
return $this->fieldset[$model]['fields'][$field];
} else {
return isset($object->hasAndBelongsToMany[$field]) ? array('type' => 'multiple') : null;
}
}

if ($key === 'validates' && !isset($this->fieldset[$object->alias]['validates'])) {
if ($key === 'validates' && !isset($this->fieldset[$model]['validates'])) {
$validates = array();
if (!empty($object->validate)) {
foreach ($object->validate as $validateField => $validateProperties) {
Expand All @@ -182,15 +182,15 @@ protected function _introspectModel($model, $key, $field = null) {
}
}
}
$this->fieldset[$object->alias]['validates'] = $validates;
$this->fieldset[$model]['validates'] = $validates;
}

if ($key === 'validates') {
if (empty($field)) {
return $this->fieldset[$object->alias]['validates'];
return $this->fieldset[$model]['validates'];
} else {
return isset($this->fieldset[$object->alias]['validates'][$field]) ?
$this->fieldset[$object->alias]['validates'] : null;
return isset($this->fieldset[$model]['validates'][$field]) ?
$this->fieldset[$model]['validates'] : null;
}
}
}
Expand Down Expand Up @@ -593,74 +593,72 @@ public function error($field, $text = null, $options = array()) {
$defaults = array('wrap' => true, 'class' => 'error-message', 'escape' => true);
$options = array_merge($defaults, $options);
$this->setEntity($field);

if ($error = $this->tagIsInvalid()) {
if (is_array($text)) {
if (isset($text['attributes']) && is_array($text['attributes'])) {
$options = array_merge($options, $text['attributes']);
unset($text['attributes']);
}
$tmp = array();
foreach ($error as &$e) {
if (isset($text[$e])) {
$tmp []= $text[$e];
} else {
$tmp []= $e;
}
if (!$error = $this->tagIsInvalid()) {
return null;
}
if (is_array($text)) {
if (isset($text['attributes']) && is_array($text['attributes'])) {
$options = array_merge($options, $text['attributes']);
unset($text['attributes']);
}
$tmp = array();
foreach ($error as &$e) {
if (isset($text[$e])) {
$tmp []= $text[$e];
} else {
$tmp []= $e;
}
$text = $tmp;
}
$text = $tmp;
}

if ($text != null) {
$error = $text;
}
if (is_array($error)) {
foreach ($error as &$e) {
if (is_numeric($e)) {
$e = __d('cake', 'Error in field %s', Inflector::humanize($this->field()));
}
if ($text != null) {
$error = $text;
}
if (is_array($error)) {
foreach ($error as &$e) {
if (is_numeric($e)) {
$e = __d('cake', 'Error in field %s', Inflector::humanize($this->field()));
}
}
if ($options['escape']) {
$error = h($error);
unset($options['escape']);
}
if (is_array($error)) {
if (count($error) > 1) {
$listParams = array();
if (isset($options['listOptions'])) {
if (is_string($options['listOptions'])) {
$listParams []= $options['listOptions'];
}
if ($options['escape']) {
$error = h($error);
unset($options['escape']);
}
if (is_array($error)) {
if (count($error) > 1) {
$listParams = array();
if (isset($options['listOptions'])) {
if (is_string($options['listOptions'])) {
$listParams []= $options['listOptions'];
} else {
if (isset($options['listOptions']['itemOptions'])) {
$listParams []= $options['listOptions']['itemOptions'];
unset($options['listOptions']['itemOptions']);
} else {
if (isset($options['listOptions']['itemOptions'])) {
$listParams []= $options['listOptions']['itemOptions'];
unset($options['listOptions']['itemOptions']);
} else {
$listParams []= array();
}
if (isset($options['listOptions']['tag'])) {
$listParams []= $options['listOptions']['tag'];
unset($options['listOptions']['tag']);
}
array_unshift($listParams, $options['listOptions']);
$listParams []= array();
}
unset($options['listOptions']);
if (isset($options['listOptions']['tag'])) {
$listParams []= $options['listOptions']['tag'];
unset($options['listOptions']['tag']);
}
array_unshift($listParams, $options['listOptions']);
}
array_unshift($listParams, $error);
$error = call_user_func_array(array($this->Html, 'nestedList'), $listParams);
} else {
$error = array_pop($error);
unset($options['listOptions']);
}
}
if ($options['wrap']) {
$tag = is_string($options['wrap']) ? $options['wrap'] : 'div';
unset($options['wrap']);
return $this->Html->tag($tag, $error, $options);
array_unshift($listParams, $error);
$error = call_user_func_array(array($this->Html, 'nestedList'), $listParams);
} else {
return $error;
$error = array_pop($error);
}
}
if ($options['wrap']) {
$tag = is_string($options['wrap']) ? $options['wrap'] : 'div';
unset($options['wrap']);
return $this->Html->tag($tag, $error, $options);
} else {
return null;
return $error;
}
}

Expand Down

0 comments on commit 1941be6

Please sign in to comment.