Enabling mutli-record validation errors in FormHelper, fixes #4076

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6540 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-03-09 18:28:38 +00:00
parent c64d4a88f0
commit 98831d27fe
3 changed files with 45 additions and 10 deletions

View file

@ -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.

View file

@ -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() {

View file

@ -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('/<div[^<>]*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('/<div[^<>]*class="error-message"[^<>]*>You must provide a URL<\/div>/', $result);
}
function testFormInput() {
$result = $this->Form->input('Contact.email', array('id' => 'custom'));
$expected = '<div class="input"><label for="custom">Email</label><input name="data[Contact][email]" type="text" id="custom" value="" /></div>';