Continuing work on Helper::setEntity and associated methods

This commit is contained in:
José Lorenzo Rodríguez 2009-10-21 08:38:20 -04:30 committed by mark_story
parent 2941e89da3
commit 50ec80ff50
6 changed files with 77 additions and 16 deletions

0
app/tmp/cache/models/empty vendored Executable file
View file

0
app/tmp/cache/persistent/empty vendored Executable file
View file

0
app/tmp/cache/views/empty vendored Executable file
View file

View file

@ -362,7 +362,7 @@ class Helper extends Overloadable {
if ($setScope) { if ($setScope) {
$view->modelScope = false; $view->modelScope = false;
} elseif (join('.', $view->entity()) == $entity) { } elseif (!empty($view->entityPath) && $view->entityPath == $entity) {
return; return;
} }
@ -371,9 +371,11 @@ class Helper extends Overloadable {
$view->association = null; $view->association = null;
$view->modelId = null; $view->modelId = null;
$view->modelScope = false; $view->modelScope = false;
$view->entityPath = null;
return; return;
} }
$view->entityPath = $entity;
$model = $view->model; $model = $view->model;
$sameScope = $hasField = false; $sameScope = $hasField = false;
$parts = array_values(Set::filter(explode('.', $entity), true)); $parts = array_values(Set::filter(explode('.', $entity), true));
@ -538,14 +540,24 @@ class Helper extends Overloadable {
* @return boolean True on errors. * @return boolean True on errors.
*/ */
function tagIsInvalid($model = null, $field = null, $modelID = null) { function tagIsInvalid($model = null, $field = null, $modelID = null) {
$view =& ClassRegistry::getObject('view');
foreach (array('model', 'field', 'modelID') as $key) { foreach (array('model', 'field', 'modelID') as $key) {
if (empty(${$key})) { if (empty(${$key})) {
${$key} = $this->{$key}(); ${$key} = $this->{$key}();
} }
} }
$view =& ClassRegistry::getObject('view');
$errors = $this->validationErrors; $errors = $this->validationErrors;
if (!empty($view->entityPath)) {
$check = $view->entityPath;
$path = explode('.',$check);
if (count($path) == 1 || is_numeric($path[0])) {
$check = $model . '.' . $check;
}
return Set::extract($errors,$check);
}
if ($view->model !== $model && isset($errors[$view->model][$model])) { if ($view->model !== $model && isset($errors[$view->model][$model])) {
$errors = $errors[$view->model]; $errors = $errors[$view->model];
} }
@ -594,7 +606,6 @@ class Helper extends Overloadable {
*/ */
function __name($options = array(), $field = null, $key = 'name') { function __name($options = array(), $field = null, $key = 'name') {
$view =& ClassRegistry::getObject('view'); $view =& ClassRegistry::getObject('view');
if ($options === null) { if ($options === null) {
$options = array(); $options = array();
} elseif (is_string($options)) { } elseif (is_string($options)) {
@ -615,7 +626,18 @@ class Helper extends Overloadable {
$name = $field; $name = $field;
break; break;
default: default:
$name = 'data[' . join('][', $view->entity()) . ']'; $entity = $view->entity();
if (!empty($view->entityPath)) {
$check = $view->entityPath;
$path = explode('.',$check);
$model = $this->model();
if ((count($path) == 1 && $model != $this->field()) || is_numeric($path[0])) {
debug($model); debug($this->field());
array_unshift($path,$model);
}
$entity = $path;
}
$name = 'data[' . join('][', $path) . ']';
break; break;
} }
@ -643,25 +665,35 @@ class Helper extends Overloadable {
$options = 0; $options = 0;
} }
if (!empty($field)) {
$this->setEntity($field);
}
if (is_array($options) && isset($options[$key])) { if (is_array($options) && isset($options[$key])) {
return $options; return $options;
} }
if (!empty($field)) {
$this->setEntity($field);
}
$view =& ClassRegistry::getObject('view');
$result = null; $result = null;
$modelName = $this->model(); $modelName = $this->model();
$fieldName = $this->field(); $fieldName = $this->field();
$modelID = $this->modelID(); $modelID = $this->modelID();
if (!empty($this->data) && !empty($view->entityPath)) {
$check = $view->entityPath;
$path = explode('.',$check);
if ((count($path) == 1 && $this->model() != $this->field()) || is_numeric($path[0])) {
$field = $this->model() . '.' . $check;
}
$result = Set::extract($this->data,$check);
}
if (is_null($fieldName)) { if (is_null($fieldName)) {
$fieldName = $modelName; $fieldName = $modelName;
$modelName = null; $modelName = null;
} }
if (isset($this->data[$fieldName]) && $modelName === null) { if (isset($this->data[$fieldName]) && $modelName === null) {
$result = $this->data[$fieldName]; $result = $this->data[$fieldName];
} elseif (isset($this->data[$modelName][$fieldName])) { } elseif (isset($this->data[$modelName][$fieldName])) {
@ -676,7 +708,6 @@ class Helper extends Overloadable {
} }
if (is_array($result)) { if (is_array($result)) {
$view =& ClassRegistry::getObject('view');
if (array_key_exists($view->fieldSuffix, $result)) { if (array_key_exists($view->fieldSuffix, $result)) {
$result = $result[$view->fieldSuffix]; $result = $result[$view->fieldSuffix];
} }

View file

@ -629,6 +629,22 @@ class HelperTest extends CakeTestCase {
$this->assertEqual($this->View->field,'created'); $this->assertEqual($this->View->field,'created');
$this->assertEqual($this->View->modelId,1); $this->assertEqual($this->View->modelId,1);
$this->assertEqual($this->View->fieldSuffix,'year'); $this->assertEqual($this->View->fieldSuffix,'year');
$this->Helper->data['HelperTestPost'][2]['HelperTestComment'][1]['title'] = 'My Title';
$result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.title');
$this->assertEqual($result,'My Title');
$this->Helper->data['HelperTestPost'][2]['HelperTestComment'][1]['created']['year'] = 2008;
$result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.created.year');
$this->assertEqual($result,2008);
$this->Helper->data[2]['HelperTestComment'][1]['created']['year'] = 2008;
$result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.created.year');
$this->assertEqual($result,2008);
$this->Helper->data['HelperTestPost']['title'] = 'My Title';
$result = $this->Helper->value('title');
$this->assertEqual($result,'My Title');
} }

View file

@ -1477,7 +1477,7 @@ class FormHelperTest extends CakeTestCase {
'label' => array('for'), 'label' => array('for'),
'preg:/[^<]+/', 'preg:/[^<]+/',
'/label', '/label',
'input' => array('type' => 'text', 'name', 'value' => '', 'id', 'class' => 'form-error'), 'input' => array('type' => 'text', 'name', 'value' => '', 'id'),
'/div' '/div'
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
@ -1602,7 +1602,7 @@ class FormHelperTest extends CakeTestCase {
'/option', '/option',
'/select', '/select',
'/div' '/div'
); ); debug($result,true);
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$result = $this->Form->input('email', array( $result = $this->Form->input('email', array(
@ -5475,5 +5475,19 @@ class FormHelperTest extends CakeTestCase {
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
} }
function testMultiRecordForm() {
$this->Form->create('ValidateProfile');
$this->Form->validationErrors['ValidateProfile'][2]['ValidateItem'][1]['name'] = 'Error in field name';
$result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
$this->Form->validationErrors['ValidateProfile'][2]['city'] = 'Error in field city';
$result = $this->Form->error('ValidateProfile.2.city');
$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
$result = $this->Form->error('2.city');
$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
}
} }
?> ?>