Permalink
Browse files

Merge pull request #220 from bar/1.3-fix-input-error-complete

Form input elements can now be rendered with 'error' class appended and empty error messages.
  • Loading branch information...
2 parents 7f2a4e5 + ae8dd1c commit 314948609dfe4600cf32b4f86e28cff3bae590ee @lorenzo lorenzo committed Sep 29, 2011
Showing with 62 additions and 4 deletions.
  1. +1 −1 cake/libs/view/helper.php
  2. +3 −2 cake/libs/view/helpers/form.php
  3. +58 −1 cake/tests/cases/libs/view/helpers/form.test.php
@@ -756,7 +756,7 @@ function _initInputField($field, $options = array()) {
$options = $this->_name($options);
$options = $this->value($options);
$options = $this->domId($options);
- if ($this->tagIsInvalid()) {
+ if ($this->tagIsInvalid() !== null) {
$options = $this->addClass($options, 'form-error');
}
return $options;
@@ -490,7 +490,8 @@ function error($field, $text = null, $options = array()) {
$options = array_merge($defaults, $options);
$this->setEntity($field);
- if ($error = $this->tagIsInvalid()) {
+ $error = $this->tagIsInvalid();
+ if ($error !== null) {
if (is_array($error)) {
list(,,$field) = explode('.', $field);
if (isset($error[$field])) {
@@ -512,7 +513,7 @@ function error($field, $text = null, $options = array()) {
unset($options[$error]);
}
- if ($text != null) {
+ if ($text !== null) {
$error = $text;
} elseif (is_numeric($error)) {
$error = sprintf(__('Error in field %s', true), Inflector::humanize($this->field()));
@@ -1297,6 +1297,7 @@ function testDisableSecurityUsingForm() {
);
$this->assertEqual($result, $expected);
}
+
/**
* testPasswordValidation method
*
@@ -1326,6 +1327,62 @@ function testPasswordValidation() {
}
/**
+ * testEmptyErrorValidation method
+ *
+ * test validation error div when validation message is an empty string
+ *
+ * @access public
+ * @return void
+ */
+ function testEmptyErrorValidation() {
+ $this->Form->validationErrors['Contact']['password'] = '';
+ $result = $this->Form->input('Contact.password');
+ $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'
+ ),
+ array('div' => array('class' => 'error-message')),
+ array(),
+ '/div',
+ '/div'
+ );
+ $this->assertTags($result, $expected);
+ }
+
+/**
+ * testEmptyInputErrorValidation method
+ *
+ * test validation error div when validation message is overriden by an empty string when calling input()
+ *
+ * @access public
+ * @return void
+ */
+ 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'),
+ 'label' => array('for' => 'ContactPassword'),
+ 'Password',
+ '/label',
+ 'input' => array(
+ 'type' => 'password', 'name' => 'data[Contact][password]',
+ 'id' => 'ContactPassword', 'class' => 'form-error'
+ ),
+ array('div' => array('class' => 'error-message')),
+ array(),
+ '/div',
+ '/div'
+ );
+ $this->assertTags($result, $expected);
+ }
+
+/**
* testFormValidationAssociated method
*
* test display of form errors in conjunction with model::validates.
@@ -5804,7 +5861,7 @@ public function testCreateOnSubmit() {
$expected = array(
'form' => array(
- 'id' => 'ContactAddForm', 'method' => 'post',
+ 'id' => 'ContactAddForm', 'method' => 'post',
'onsubmit' => 'someFunction();event.returnValue = false; return false;',
'action' => '/contacts/index/param',
'accept-charset' => 'utf-8'

0 comments on commit 3149486

Please sign in to comment.