diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 8cffc7f9f..9c6ac061f 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -215,6 +215,7 @@ class FormHelper extends AppHelper { */ function error($field, $text = null, $options = array()) { $this->setFormTag($field); + $options = am(array('wrap' => true, 'class' => 'error-message', 'escape' => true), $options); if ($error = $this->tagIsInvalid()) { if ($text != null) { @@ -222,7 +223,14 @@ class FormHelper extends AppHelper { } elseif (is_numeric($error)) { $error = 'Error in field ' . Inflector::humanize($this->field()); } - return $this->Html->div('error-message', $error); + if ($options['escape']) { + $error = h($error); + } + if ($options['wrap'] === true) { + return $this->Html->div($options['class'], $error); + } else { + return $error; + } } else { return null; } diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index 518897542..6a83a1568 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -144,6 +144,28 @@ class FormHelperTest extends UnitTestCase { $this->assertNoPattern('/]+[^type|name|id|value|class]=[^<>]*>/', $result); } + function testFieldError() { + $this->Form->validationErrors['Model']['field'] = 1; + $result = $this->Form->error('Model.field'); + $this->assertEqual($result, '
'); + + $result = $this->Form->error('Model.field', null, array('wrap' => false)); + $this->assertEqual($result, 'Error in field Field'); + + $this->Form->validationErrors['Model']['field'] = "This field contains invalid input"; + $result = $this->Form->error('Model.field', null, array('wrap' => false)); + $this->assertEqual($result, 'This field contains invalid input'); + + $result = $this->Form->error('Model.field', "Badness!", array('wrap' => false)); + $this->assertEqual($result, '<strong>Badness!</strong>'); + + $result = $this->Form->error('Model.field', "Badness!", array('wrap' => false, 'escape' => true)); + $this->assertEqual($result, '<strong>Badness!</strong>'); + + $result = $this->Form->error('Model.field', "Badness!", array('wrap' => false, 'escape' => false)); + $this->assertEqual($result, 'Badness!'); + } + function testPassword() { $result = $this->Form->password('Model/field'); $expected = '';