Permalink
Browse files

Fixing FormHelper::error to no add error names and messages as html a…

…ttributes. Fixes #585

This introduces the special key 'attributes' to FormHelper::error() when passed in the second parameter
The contents of said key will be converted into html attributes for the error container tag
  • Loading branch information...
1 parent 0300fc1 commit 23eea00a0bad28d2ec0efdd744b08a5637395a81 @lorenzo lorenzo committed Apr 20, 2010
Showing with 33 additions and 3 deletions.
  1. +5 −2 cake/libs/view/helpers/form.php
  2. +28 −1 cake/tests/cases/libs/view/helpers/form.test.php
@@ -494,9 +494,12 @@ function error($field, $text = null, $options = array()) {
$error--;
}
if (is_array($text)) {
- $options = array_merge($options, $text);
+ $options = array_merge($options, array_intersect_key($text, $defaults));
+ if (isset($text['attributes']) && is_array($text['attributes'])) {
+ $options = array_merge($options, $text['attributes']);
+ }
$text = isset($text[$error]) ? $text[$error] : null;
- unset($options[$error]);
+ unset($options[$error]);
}
if ($text != null) {
@@ -1776,7 +1776,12 @@ function testInput() {
$this->assertTags($result, $expected);
$this->Form->validationErrors['Model']['field'] = 'minLength';
- $result = $this->Form->input('Model.field', array('error' => array('minLength' => __('Le login doit contenir au moins 2 caractères', true))));
+ $result = $this->Form->input('Model.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'),
@@ -1789,6 +1794,28 @@ function testInput() {
'/div'
);
$this->assertTags($result, $expected);
+
+ $this->Form->validationErrors['Model']['field'] = 'maxLength';
+ $result = $this->Form->input('Model.field', array(
+ 'error' => array(
+ 'wrap' => 'span',
+ 'attributes' => array('rel' => 'fake'),
+ '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'),
+ 'Field',
+ '/label',
+ 'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField', 'class' => 'form-error'),
+ array('span' => array('class' => 'error-message', 'rel' => 'fake')),
+ 'login too large',
+ '/span',
+ '/div'
+ );
+ $this->assertTags($result, $expected);
}
/**
* test form->input() with datetime, date and time types

0 comments on commit 23eea00

Please sign in to comment.