Skip to content
Permalink
Browse files

Fix FormHelper::tagIsInvalid with saveMany forms.

When saving multiple records validation errors were not correctly shown.
Fudge the entity path so it matches the validation errors set in the
models.

Fixes #3828
  • Loading branch information...
markstory committed May 14, 2013
1 parent bd3428e commit f7d106a38642363ef8a61bf7e479260bfe7662c0
Showing with 46 additions and 2 deletions.
  1. +39 −2 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +7 −0 lib/Cake/View/Helper/FormHelper.php
@@ -1456,11 +1456,10 @@ public function testUnlockFieldRemovingFromFields() {
*/
public function testTagIsInvalid() {
$Contact = ClassRegistry::getObject('Contact');
$Contact->validationErrors[0]['email'] = array('Please provide an email');
$Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
$this->Form->setEntity('Contact.0.email');
$result = $this->Form->tagIsInvalid();
$expected = array('Please provide an email');
$this->assertEquals($expected, $result);
$this->Form->setEntity('Contact.1.email');
@@ -1472,6 +1471,26 @@ public function testTagIsInvalid() {
$this->assertFalse($result);
}
/**
* Test tagIsInvalid with validation errors from a saveMany
*
* @return void
*/
public function testTagIsInvalidSaveMany() {
$Contact = ClassRegistry::getObject('Contact');
$Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
$this->Form->create('Contact');
$this->Form->setEntity('0.email');
$result = $this->Form->tagIsInvalid();
$this->assertEquals($expected, $result);
$this->Form->setEntity('0.Contact.email');
$result = $this->Form->tagIsInvalid();
$this->assertEquals($expected, $result);
}
/**
* Test validation errors.
*
@@ -8486,6 +8505,24 @@ public function testMultiRecordFormValidationErrors() {
$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
}
/**
* test the correct display of multi-record form validation errors.
*
* @return void
*/
public function testSaveManyRecordFormValidationErrors() {
$this->Form->create('ValidateUser');
$ValidateUser = ClassRegistry::getObject('ValidateUser');
$ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
$result = $this->Form->error('0.ValidateUser.ValidateItem.name');
$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
$ValidateUser->validationErrors[0]['city'] = array('Error in field city');
$result = $this->Form->error('ValidateUser.0.city');
$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
}
/**
* tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
*
@@ -272,6 +272,13 @@ protected function _isRequiredField($validationRules) {
public function tagIsInvalid() {
$entity = $this->entity();
$model = array_shift($entity);
// 0.Model.field. Fudge entity path
if (empty($model) || is_numeric($model)) {
array_splice($entity, 1, 0, $model);
$model = array_shift($entity);
}
$errors = array();
if (!empty($entity) && isset($this->validationErrors[$model])) {
$errors = $this->validationErrors[$model];

0 comments on commit f7d106a

Please sign in to comment.
You can’t perform that action at this time.