Merge pull request #10378 from mvdriel/make-error-class-in-formhelper-configurable

Make error class of div in FormHelper::input configurable
This commit is contained in:
Mark Story 2017-03-17 22:48:50 -04:00 committed by GitHub
commit 83e5ae522e
2 changed files with 70 additions and 2 deletions

View file

@ -10734,4 +10734,72 @@ class FormHelperTest extends CakeTestCase {
$this->assertAttributeEquals($here, '_lastAction', $this->Form, "_lastAction shouldn't be empty."); $this->assertAttributeEquals($here, '_lastAction', $this->Form, "_lastAction shouldn't be empty.");
} }
/**
* Tests the 'errorClass' option when error is returned.
*
* @return void
*/
public function testInputErrorClass() {
$Contact = ClassRegistry::getObject('Contact');
$Contact->validationErrors['field'] = array('Badness!');
$result = $this->Form->input('Contact.field', array(
'type' => 'text',
'div' => array('errorClass' => 'has-error')
));
$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-message')),
'Badness!',
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.field', array(
'type' => 'text',
'error' => array('attributes' => array('class' => 'error'))
));
$expected = array(
'div' => array('class' => 'input text 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.field', 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' => 'ContactField'),
'Field',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Contact][field]',
'id' => 'ContactField', 'class' => 'form-error'
),
array('div' => array('class' => 'form-control-feedback')),
'Badness!',
'/div'
);
$this->assertTags($result, $expected);
}
} }

View file

@ -1068,7 +1068,7 @@ class FormHelper extends AppHelper {
if ($type !== 'hidden' && $error !== false) { if ($type !== 'hidden' && $error !== false) {
$errMsg = $this->error($fieldName, $error); $errMsg = $this->error($fieldName, $error);
if ($errMsg) { if ($errMsg) {
$divOptions = $this->addClass($divOptions, 'error'); $divOptions = $this->addClass($divOptions, Hash::get($divOptions, 'errorClass', 'error'));
if ($errorMessage) { if ($errorMessage) {
$out['error'] = $errMsg; $out['error'] = $errMsg;
} }
@ -1088,7 +1088,7 @@ class FormHelper extends AppHelper {
if (!empty($divOptions['tag'])) { if (!empty($divOptions['tag'])) {
$tag = $divOptions['tag']; $tag = $divOptions['tag'];
unset($divOptions['tag']); unset($divOptions['tag'], $divOptions['errorClass']);
$output = $this->Html->tag($tag, $output, $divOptions); $output = $this->Html->tag($tag, $output, $divOptions);
} }
return $output; return $output;