diff --git a/cake/libs/view/helper.php b/cake/libs/view/helper.php index edd0dfbac..cac8134ff 100644 --- a/cake/libs/view/helper.php +++ b/cake/libs/view/helper.php @@ -31,7 +31,7 @@ /** * Included libs */ -uses('overloadable'); +App::import('Core', 'Overloadable'); /** * Backend for helpers. @@ -452,18 +452,29 @@ class Helper extends Overloadable { /** * Returns false if given FORM field has no errors. Otherwise it returns the constant set in the array Model->validationErrors. * - * @param string $model Model name as string + * @param string $model Model name as string * @param string $field Fieldname as string + * @param integer $modelID Unique index identifying this record within the form * @return boolean True on errors. */ - function tagIsInvalid($model = null, $field = null) { - if ($model == null) { - $model = $this->model(); + function tagIsInvalid($model = null, $field = null, $modelID = null) { + foreach (array('model', 'field', 'modelID') as $key) { + if (empty(${$key})) { + ${$key} = $this->{$key}(); + } } - if ($field == null) { - $field = $this->field(); + $view =& ClassRegistry::getObject('view'); + $errors = $this->validationErrors; + + if ($view->model !== $model && isset($errors[$view->model][$model])) { + $errors = $errors[$view->model]; + } + + if (empty($modelID)) { + return empty($errors[$model][$field]) ? 0 : $errors[$model][$field]; + } else { + return empty($errors[$model][$modelID][$field]) ? 0 : $errors[$model][$modelID][$field]; } - return empty($this->validationErrors[$model][$field]) ? 0 : $this->validationErrors[$model][$field]; } /** * Generates a DOM ID for the selected element, if one is not set. diff --git a/cake/tests/cases/libs/view/helper.test.php b/cake/tests/cases/libs/view/helper.test.php index c1215765a..868ae88d4 100644 --- a/cake/tests/cases/libs/view/helper.test.php +++ b/cake/tests/cases/libs/view/helper.test.php @@ -113,6 +113,10 @@ class HelperTest extends UnitTestCase { } function testFormFieldNameParsing() { + // PHP4 reference hack + ClassRegistry::removeObject('view'); + ClassRegistry::addObject('view', $this->View); + $this->Helper->setEntity('HelperTestPost.id'); $this->assertFalse($this->View->modelScope); $this->assertEqual($this->View->model, 'HelperTestPost'); @@ -240,6 +244,10 @@ class HelperTest extends UnitTestCase { } function testFieldsWithSameName() { + // PHP4 reference hack + ClassRegistry::removeObject('view'); + ClassRegistry::addObject('view', $this->View); + $this->Helper->setEntity('HelperTestTag', true); $this->Helper->setEntity('HelperTestTag.id'); @@ -266,6 +274,9 @@ class HelperTest extends UnitTestCase { } function testFieldSameAsModel() { + // PHP4 reference hack + ClassRegistry::removeObject('view'); + ClassRegistry::addObject('view', $this->View); $this->Helper->setEntity('HelperTestTag', true); @@ -279,6 +290,10 @@ class HelperTest extends UnitTestCase { } function testFieldSuffixForDate() { + // PHP4 reference hack + ClassRegistry::removeObject('view'); + ClassRegistry::addObject('view', $this->View); + $this->Helper->setEntity('HelperTestPost', true); $this->assertEqual($this->View->model, 'HelperTestPost'); $this->assertEqual($this->View->field, null); @@ -292,7 +307,6 @@ class HelperTest extends UnitTestCase { $this->assertEqual($this->View->modelId, null); $this->assertEqual($this->View->association, null); $this->assertEqual($this->View->fieldSuffix, 'month'); - } function testMulitDimensionValue() { diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index 0b9ca00af..e2388de13 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -353,7 +353,6 @@ class FormHelperTest extends CakeTestCase { 'ValidateUser' => array('email' => 1), 'ValidateProfile' => array('full_name' => 1, 'city' => 1) ); - $this->assertEqual($this->Form->validationErrors, $expected); unset($this->ValidateUser->ValidateProfile); @@ -391,6 +390,17 @@ class FormHelperTest extends CakeTestCase { unset($this->ValidateUser); } + function testFormValidationMultiRecord() { + $this->Form->validationErrors['Contact'] = array(2 => array('name' => 'This field cannot be left blank')); + $result = $this->Form->input('Contact.2.name'); + $this->assertPattern('/]*class="error-message"[^<>]*>This field cannot be left blank<\/div>/', $result); + + $this->Form->validationErrors['UserForm'] = array('OpenidUrl' => array('url' => 'You must provide a URL')); + $this->Form->create('UserForm'); + $result = $this->Form->input('OpenidUrl.url'); + $this->assertPattern('/]*class="error-message"[^<>]*>You must provide a URL<\/div>/', $result); + } + function testFormInput() { $result = $this->Form->input('Contact.email', array('id' => 'custom')); $expected = '
';