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
This commit is contained in:
nate 2007-03-23 16:06:48 +00:00
parent f8023ff0dc
commit 88563ebe46
2 changed files with 31 additions and 1 deletions

View file

@ -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;
}

View file

@ -144,6 +144,28 @@ class FormHelperTest extends UnitTestCase {
$this->assertNoPattern('/<input[^<>]+[^type|name|id|value|class]=[^<>]*>/', $result);
}
function testFieldError() {
$this->Form->validationErrors['Model']['field'] = 1;
$result = $this->Form->error('Model.field');
$this->assertEqual($result, '<div class="error-message">Error in field Field</div>');
$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', "<strong>Badness!</strong>", array('wrap' => false));
$this->assertEqual($result, '&lt;strong&gt;Badness!&lt;/strong&gt;');
$result = $this->Form->error('Model.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
$this->assertEqual($result, '&lt;strong&gt;Badness!&lt;/strong&gt;');
$result = $this->Form->error('Model.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
$this->assertEqual($result, '<strong>Badness!</strong>');
}
function testPassword() {
$result = $this->Form->password('Model/field');
$expected = '<input name="data[Model][field]" type="password" value="" id="ModelField" />';