Skip to content

Loading…

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

Merged
merged 1 commit into from

4 participants

@bar

From #863 (comment)

The idea is to complete [1] and [2] which enabled to trigger element errors (field coloring) but not rendering error messages.

Actually, calling Form->input() with:
'error' => false prevent any kind of error reporting (field error coloring AND error messages)
'error' => '' allow field error coloring AND render an HTML element with and empty error message

IMO,
'error' => '' should allow field error coloring BUT prevent rendering an HTML element with and empty error message.

[1] 9f23f65
[2] ae8dd1c

@markstory
CakePHP member

Any objections to this? I think its reasonable for 2.3 It adds a bit more sensitive to the error key and empty values, but its already kind of complicated.

@lorenzo
CakePHP member

That is kind of complicated and magic I would never use an empty string as a configure value for anything. Is there a way we can change it to something more meaningful?

@bar
bar commented

@lorenzo you mean, using two variables?

@lorenzo
CakePHP member

Yeah, that might be the only solution without making option handling complicated

@bar
bar commented

:+1: for that

I'm thinking something like:
'error' => false prevent any kind of error reporting (field error coloring AND error messages)
'errorMessage' => false (default: true) allow field error coloring BUT prevent rendering an HTML element with an empty error message.

@markstory
CakePHP member

I like the idea of an additional option as well. This makes the intent clearer and doesn't add additional behavior to various falsey values.

@bar

Rebased to 2.3 and updated as discussed :)

@bar

I'll update the docs if this goes through.

@ADmad ADmad commented on an outdated diff
lib/Cake/View/Helper/FormHelper.php
((6 lines not shown))
* - `div` - Either `false` to disable the div, or an array of options for the div.
* See HtmlHelper::div() for more options.
- * - `options` - for widgets that take options e.g. radio, select
- * - `error` - control the error message that is produced
+ * - `options` - For widgets that take options e.g. radio, select.
+ * - `error` - Control the error message that is produced. Set to `false` to disable any kind of error reporting (field
+ * error coloring and error messages).
+ * - `errorMessage` - Boolean to control rendering error messages (field error coloring will still occur).
@ADmad CakePHP member
ADmad added a note

"coloring" is not an accurate word. The wrapper div gets the error class, you could have any styling for that class.

@bar
bar added a note

Ohh man, feature blindness on that one!
Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bar bar Calling Form->input() with 'errorMessage'=>false should trigger field
error, but not render error message (HTML element).
1c281c9
@bar

Updated again, missed a 'color' :P

@lorenzo
CakePHP member

I think this makes sense, merging this one

@lorenzo lorenzo merged commit dc37082 into cakephp:2.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 16, 2012
  1. @bar

    Calling Form->input() with 'errorMessage'=>false should trigger field

    bar committed
    error, but not render error message (HTML element).
Showing with 59 additions and 14 deletions.
  1. +47 −9 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +12 −5 lib/Cake/View/Helper/FormHelper.php
View
56 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
@@ -1527,9 +1527,7 @@ public function testTagIsInvalid() {
}
/**
- * testPasswordValidation method
- *
- * test validation errors on password input.
+ * Test validation errors.
*
* @return void
*/
@@ -1553,18 +1551,31 @@ public function testPasswordValidation() {
'/div'
);
$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 error div when validation message is an empty string
+ * Test validation errors, when validation message is an empty string.
*
* @access public
* @return void
*/
public function testEmptyErrorValidation() {
$this->Form->validationErrors['Contact']['password'] = '';
+
$result = $this->Form->input('Contact.password');
$expected = array(
'div' => array('class' => 'input password error'),
@@ -1581,18 +1592,31 @@ public function testEmptyErrorValidation() {
'/div'
);
$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 error div when validation message is overridden by an empty string when calling input()
+ * Test validation errors, when calling input() overriding validation message by an empty string.
*
* @access public
* @return void
*/
public function testEmptyInputErrorValidation() {
$this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
+
$result = $this->Form->input('Contact.password', array('error' => ''));
$expected = array(
'div' => array('class' => 'input password error'),
@@ -1609,6 +1633,20 @@ public function testEmptyInputErrorValidation() {
'/div'
);
$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);
}
/**
View
17 lib/Cake/View/Helper/FormHelper.php
@@ -912,16 +912,18 @@ public function inputs($fields = null, $blacklist = null) {
* will be treated as a regular html attribute for the generated input.
*
* - `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.
* See HtmlHelper::div() for more options.
- * - `options` - for widgets that take options e.g. radio, select
- * - `error` - control the error message that is produced
+ * - `options` - For widgets that take options e.g. radio, select.
+ * - `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.
* - `before` - Content to place before the label + input.
* - `after` - Content to place after 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 checkbox format order: array('before', 'input', 'between', 'label', 'after', 'error')
* - Hidden input will not be formatted
@@ -1070,6 +1072,9 @@ public function input($fieldName, $options = array()) {
$error = $this->_extractOption('error', $options, null);
unset($options['error']);
+ $errorMessage = $this->_extractOption('errorMessage', $options, true);
+ unset($options['errorMessage']);
+
$selected = $this->_extractOption('selected', $options, null);
unset($options['selected']);
@@ -1149,7 +1154,9 @@ public function input($fieldName, $options = array()) {
$errMsg = $this->error($fieldName, $error);
if ($errMsg) {
$divOptions = $this->addClass($divOptions, 'error');
- $out['error'] = $errMsg;
+ if ($errorMessage) {
+ $out['error'] = $errMsg;
+ }
}
}
Something went wrong with that request. Please try again.