Permalink
Browse files

Make error class of div in FormHelper::input configurable

  • Loading branch information...
mvdriel committed Mar 8, 2017
1 parent 8d0e1fa commit 4728586365f58b4d98000942efa135c5dc0b31d8
Showing with 68 additions and 1 deletion.
  1. +66 −0 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +2 −1 lib/Cake/View/Helper/FormHelper.php
@@ -10734,4 +10734,70 @@ public function testLastActionWithNamedString() {
$this->assertAttributeEquals($here, '_lastAction', $this->Form, "_lastAction shouldn't be empty.");
}
/**
* Tests the 'errorClass' option when error is returned.
*
* @return void
*/
public function testInputErrorClass() {
$result = $this->Form->input('Contact.field', array(
'type' => 'text',
'div' => array('errorClass' => 'has-error'),
'error' => true
));
$expected = array(
'div' => array('class' => 'input text has-error'),
'label' => array('for' => 'ContactField'),
'Field',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][field]',
'id' => 'ContactField', 'class' => 'form-error'
),
array('div' => array('class' => 'error')),
'Badness!',
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.second', array(
'type' => 'text',
'error' => array('attributes' => array('class' => 'error'))
));
$expected = array(
'div' => array('class' => 'input text error'),
'label' => array('for' => 'ContactSecond'),
'Field',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][second]',
'id' => 'ContactSecond', 'class' => 'form-error'
),
array('div' => array('class' => 'error')),
'Badness!',
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.third', array(
'type' => 'text',
'div' => array('errorClass' => 'has-error'),
'error' => array('attributes' => array('class' => 'form-control-feedback'))
));
$expected = array(
'div' => array('class' => 'input text has-error'),
'label' => array('for' => 'ContactThird'),
'Field',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][third]',
'id' => 'ContactThird', 'class' => 'form-error'
),
array('div' => array('class' => 'form-control-feedback')),
'Badness!',
'/div'
);
$this->assertTags($result, $expected);
}
}
@@ -1068,7 +1068,7 @@ public function input($fieldName, $options = array()) {
if ($type !== 'hidden' && $error !== false) {
$errMsg = $this->error($fieldName, $error);
if ($errMsg) {
$divOptions = $this->addClass($divOptions, 'error');
$divOptions = $this->addClass($divOptions, Hash::get($divOptions, 'errorClass', 'error'));
if ($errorMessage) {
$out['error'] = $errMsg;
}
@@ -1089,6 +1089,7 @@ public function input($fieldName, $options = array()) {
if (!empty($divOptions['tag'])) {
$tag = $divOptions['tag'];
unset($divOptions['tag']);
unset($divOptions['errorClass']);
$output = $this->Html->tag($tag, $output, $divOptions);
}
return $output;

0 comments on commit 4728586

Please sign in to comment.