Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling Form->input() with 'error'=>'' should trigger field error, but not render error element. #918

Merged
merged 1 commit into from Dec 1, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 47 additions & 9 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -1527,9 +1527,7 @@ public function testTagIsInvalid() {
} }


/** /**
* testPasswordValidation method * Test validation errors.
*
* test validation errors on password input.
* *
* @return void * @return void
*/ */
Expand All @@ -1553,18 +1551,31 @@ public function testPasswordValidation() {
'/div' '/div'
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);

$result = $this->Form->input('Contact.password', array('errorMessage' => false));
$expected = array(
'div' => array('class' => 'input password error'),
'label' => array('for' => 'ContactPassword'),
'Password',
'/label',
'input' => array(
'type' => 'password', 'name' => 'data[Contact][password]',
'id' => 'ContactPassword', 'class' => 'form-error'
),
'/div'
);
$this->assertTags($result, $expected);
} }


/** /**
* testEmptyErrorValidation method * Test validation errors, when validation message is an empty string.
*
* test validation error div when validation message is an empty string
* *
* @access public * @access public
* @return void * @return void
*/ */
public function testEmptyErrorValidation() { public function testEmptyErrorValidation() {
$this->Form->validationErrors['Contact']['password'] = ''; $this->Form->validationErrors['Contact']['password'] = '';

$result = $this->Form->input('Contact.password'); $result = $this->Form->input('Contact.password');
$expected = array( $expected = array(
'div' => array('class' => 'input password error'), 'div' => array('class' => 'input password error'),
Expand All @@ -1581,18 +1592,31 @@ public function testEmptyErrorValidation() {
'/div' '/div'
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);

$result = $this->Form->input('Contact.password', array('errorMessage' => false));
$expected = array(
'div' => array('class' => 'input password error'),
'label' => array('for' => 'ContactPassword'),
'Password',
'/label',
'input' => array(
'type' => 'password', 'name' => 'data[Contact][password]',
'id' => 'ContactPassword', 'class' => 'form-error'
),
'/div'
);
$this->assertTags($result, $expected);
} }


/** /**
* testEmptyInputErrorValidation method * Test validation errors, when calling input() overriding validation message by an empty string.
*
* test validation error div when validation message is overridden by an empty string when calling input()
* *
* @access public * @access public
* @return void * @return void
*/ */
public function testEmptyInputErrorValidation() { public function testEmptyInputErrorValidation() {
$this->Form->validationErrors['Contact']['password'] = 'Please provide a password'; $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';

$result = $this->Form->input('Contact.password', array('error' => '')); $result = $this->Form->input('Contact.password', array('error' => ''));
$expected = array( $expected = array(
'div' => array('class' => 'input password error'), 'div' => array('class' => 'input password error'),
Expand All @@ -1609,6 +1633,20 @@ public function testEmptyInputErrorValidation() {
'/div' '/div'
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);

$result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
$expected = array(
'div' => array('class' => 'input password error'),
'label' => array('for' => 'ContactPassword'),
'Password',
'/label',
'input' => array(
'type' => 'password', 'name' => 'data[Contact][password]',
'id' => 'ContactPassword', 'class' => 'form-error'
),
'/div'
);
$this->assertTags($result, $expected);
} }


/** /**
Expand Down
17 changes: 12 additions & 5 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -912,16 +912,18 @@ public function inputs($fields = null, $blacklist = null) {
* will be treated as a regular html attribute for the generated input. * will be treated as a regular html attribute for the generated input.
* *
* - `type` - Force the type of widget you want. e.g. `type => 'select'` * - `type` - Force the type of widget you want. e.g. `type => 'select'`
* - `label` - Either a string label, or an array of options for the label. See FormHelper::label() * - `label` - Either a string label, or an array of options for the label. See FormHelper::label().
* - `div` - Either `false` to disable the div, or an array of options for the div. * - `div` - Either `false` to disable the div, or an array of options for the div.
* See HtmlHelper::div() for more options. * See HtmlHelper::div() for more options.
* - `options` - for widgets that take options e.g. radio, select * - `options` - For widgets that take options e.g. radio, select.
* - `error` - control the error message that is produced * - `error` - Control the error message that is produced. Set to `false` to disable any kind of error reporting (field
* error and error messages).
* - `errorMessage` - Boolean to control rendering error messages (field error will still occur).
* - `empty` - String or boolean to enable empty select box options. * - `empty` - String or boolean to enable empty select box options.
* - `before` - Content to place before the label + input. * - `before` - Content to place before the label + input.
* - `after` - Content to place after the label + input. * - `after` - Content to place after the label + input.
* - `between` - Content to place between the label + input. * - `between` - Content to place between the label + input.
* - `format` - format template for element order. Any element that is not in the array, will not be in the output. * - `format` - Format template for element order. Any element that is not in the array, will not be in the output.
* - Default input format order: array('before', 'label', 'between', 'input', 'after', 'error') * - Default input format order: array('before', 'label', 'between', 'input', 'after', 'error')
* - Default checkbox format order: array('before', 'input', 'between', 'label', 'after', 'error') * - Default checkbox format order: array('before', 'input', 'between', 'label', 'after', 'error')
* - Hidden input will not be formatted * - Hidden input will not be formatted
Expand Down Expand Up @@ -1070,6 +1072,9 @@ public function input($fieldName, $options = array()) {
$error = $this->_extractOption('error', $options, null); $error = $this->_extractOption('error', $options, null);
unset($options['error']); unset($options['error']);


$errorMessage = $this->_extractOption('errorMessage', $options, true);
unset($options['errorMessage']);

$selected = $this->_extractOption('selected', $options, null); $selected = $this->_extractOption('selected', $options, null);
unset($options['selected']); unset($options['selected']);


Expand Down Expand Up @@ -1149,7 +1154,9 @@ public function input($fieldName, $options = array()) {
$errMsg = $this->error($fieldName, $error); $errMsg = $this->error($fieldName, $error);
if ($errMsg) { if ($errMsg) {
$divOptions = $this->addClass($divOptions, 'error'); $divOptions = $this->addClass($divOptions, 'error');
$out['error'] = $errMsg; if ($errorMessage) {
$out['error'] = $errMsg;
}
} }
} }


Expand Down