Skip to content
Permalink
Browse files

Continuing work on Helper::setEntity and associated methods

  • Loading branch information...
lorenzo authored and markstory committed Oct 21, 2009
1 parent 2941e89 commit 50ec80ff50ee4ed655234404f8547cd406dac78c
No changes.
No changes.
No changes.
@@ -362,7 +362,7 @@ function setEntity($entity, $setScope = false) {
if ($setScope) {
$view->modelScope = false;
} elseif (join('.', $view->entity()) == $entity) {
} elseif (!empty($view->entityPath) && $view->entityPath == $entity) {
return;
}
@@ -371,9 +371,11 @@ function setEntity($entity, $setScope = false) {
$view->association = null;
$view->modelId = null;
$view->modelScope = false;
$view->entityPath = null;
return;
}
$view->entityPath = $entity;
$model = $view->model;
$sameScope = $hasField = false;
$parts = array_values(Set::filter(explode('.', $entity), true));
@@ -538,14 +540,24 @@ function field() {
* @return boolean True on errors.
*/
function tagIsInvalid($model = null, $field = null, $modelID = null) {
$view =& ClassRegistry::getObject('view');
foreach (array('model', 'field', 'modelID') as $key) {
if (empty(${$key})) {
${$key} = $this->{$key}();
}
}
$view =& ClassRegistry::getObject('view');
$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])) {
$errors = $errors[$view->model];
}
@@ -594,7 +606,6 @@ function domId($options = null, $id = 'id') {
*/
function __name($options = array(), $field = null, $key = 'name') {
$view =& ClassRegistry::getObject('view');
if ($options === null) {
$options = array();
} elseif (is_string($options)) {
@@ -615,7 +626,18 @@ function __name($options = array(), $field = null, $key = 'name') {
$name = $field;
break;
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;
}
@@ -643,25 +665,35 @@ function value($options = array(), $field = null, $key = 'value') {
$options = 0;
}
if (!empty($field)) {
$this->setEntity($field);
}
if (is_array($options) && isset($options[$key])) {
return $options;
}
if (!empty($field)) {
$this->setEntity($field);
}
$view =& ClassRegistry::getObject('view');
$result = null;
$modelName = $this->model();
$fieldName = $this->field();
$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)) {
$fieldName = $modelName;
$modelName = null;
}
if (isset($this->data[$fieldName]) && $modelName === null) {
$result = $this->data[$fieldName];
} elseif (isset($this->data[$modelName][$fieldName])) {
@@ -676,7 +708,6 @@ function value($options = array(), $field = null, $key = 'value') {
}
if (is_array($result)) {
$view =& ClassRegistry::getObject('view');
if (array_key_exists($view->fieldSuffix, $result)) {
$result = $result[$view->fieldSuffix];
}
@@ -629,6 +629,22 @@ function testMultiDimensionalField() {
$this->assertEqual($this->View->field,'created');
$this->assertEqual($this->View->modelId,1);
$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');
}
@@ -1477,7 +1477,7 @@ function testMultipleInputValidation() {
'label' => array('for'),
'preg:/[^<]+/',
'/label',
'input' => array('type' => 'text', 'name', 'value' => '', 'id', 'class' => 'form-error'),
'input' => array('type' => 'text', 'name', 'value' => '', 'id'),
'/div'
);
$this->assertTags($result, $expected);
@@ -1602,7 +1602,7 @@ function testFormInput() {
'/option',
'/select',
'/div'
);
); debug($result,true);
$this->assertTags($result, $expected);
$result = $this->Form->input('email', array(
@@ -5475,5 +5475,19 @@ function testBrokenness() {
);
$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'));
}
}
?>

0 comments on commit 50ec80f

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