From 88563ebe468dd064cfe1bf1894a49c974048900a Mon Sep 17 00:00:00 2001 From: nate Date: Fri, 23 Mar 2007 16:06:48 +0000 Subject: [PATCH] Adding test cases for FormHelper::error() git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4662 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/view/helpers/form.php | 10 ++++++++- .../cases/libs/view/helpers/form.test.php | 22 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) 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, '
Error in field Field
'); + + $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 = '';