Skip to content
Permalink
Browse files

Expanding Helper an View functionaility to accept deep nested entitit…

…ies.

This allows the form helper to render inputs with arbitrary number of dot separated parts
  • Loading branch information...
lorenzo authored and markstory committed Oct 21, 2009
1 parent 50ec80f commit ce743c00e86bfdb828cd07ed3477503fcb6385fd
@@ -541,31 +541,10 @@ function field() {
*/
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}();
}
}
$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];
}
if (!isset($modelID)) {
return empty($errors[$model][$field]) ? 0 : $errors[$model][$field];
} else {
return empty($errors[$model][$modelID][$field]) ? 0 : $errors[$model][$modelID][$field];
$entity = $view->entity();
if (!empty($entity)) {
return Set::extract($errors,join('.',$entity));
}
}
@@ -586,8 +565,10 @@ function domId($options = null, $id = 'id') {
$this->setEntity($options);
return $this->domId();
}
$dom = $this->model() . $this->modelID() . Inflector::camelize($view->field) . Inflector::camelize($view->fieldSuffix);
$entity = $view->entity();
$model = array_shift($entity);
$dom = $model . join('',array_map(array('Inflector','camelize'),$entity));
if (is_array($options) && !array_key_exists($id, $options)) {
$options[$id] = $dom;
@@ -626,18 +607,7 @@ function __name($options = array(), $field = null, $key = 'name') {
$name = $field;
break;
default:
$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) . ']';
$name = 'data[' . join('][', $view->entity()) . ']';
break;
}
@@ -676,35 +646,9 @@ function value($options = array(), $field = null, $key = 'value') {
$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])) {
$result = $this->data[$modelName][$fieldName];
} elseif (isset($this->data[$fieldName]) && is_array($this->data[$fieldName])) {
if (ClassRegistry::isKeySet($fieldName)) {
$model =& ClassRegistry::getObject($fieldName);
$result = $this->__selectedArray($this->data[$fieldName], $model->primaryKey);
}
} elseif (isset($this->data[$modelName][$modelID][$fieldName])) {
$result = $this->data[$modelName][$modelID][$fieldName];
$entity = $view->entity();
if (!empty($this->data) && !empty($entity)) {
$result = Set::extract($this->data,join('.',$entity));
}
if (is_array($result)) {
@@ -609,6 +609,19 @@ function uuid($object, $url) {
*/
function entity() {
$assoc = ($this->association) ? $this->association : $this->model;
if (!empty($this->entityPath)) {
$path = explode('.',$this->entityPath);
$count = count($path);
if (
($count == 1 && !empty($this->association)) ||
($count == 1 && $this->model != $this->entityPath) ||
($count == 2 && !empty($this->fieldSuffix)) ||
is_numeric($path[0])
) {
array_unshift($path,$assoc);
}
return Set::filter($path);
}
return array_values(Set::filter(
array($assoc, $this->modelId, $this->field, $this->fieldSuffix)
));
@@ -645,6 +645,10 @@ function testMultiDimensionalField() {
$this->Helper->data['HelperTestPost']['title'] = 'My Title';
$result = $this->Helper->value('title');
$this->assertEqual($result,'My Title');
$this->Helper->data['My']['title'] = 'My Title';
$result = $this->Helper->value('My.title');
$this->assertEqual($result,'My Title');
}
@@ -574,7 +574,7 @@ class ValidateItem extends CakeTestModel {
*/
var $_schema = array(
'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
'' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
'description' => array(
'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
@@ -709,6 +709,8 @@ function tearDown() {
unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
Configure::write('Security.salt', $this->oldSalt);
}
/**
* testFormCreateWithSecurity method
@@ -1602,7 +1604,7 @@ function testFormInput() {
'/option',
'/select',
'/div'
); debug($result,true);
);
$this->assertTags($result, $expected);
$result = $this->Form->input('email', array(
@@ -5477,17 +5479,95 @@ function testBrokenness() {
}
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'));
$this->Form->create('ValidateProfile');
$this->Form->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
$result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
$expected = array(
'div' => array('class' => 'input textarea'),
'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
'Name',
'/label',
'textarea' => array(
'id' => 'ValidateProfile1ValidateItem2Name',
'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
'cols' => 30,
'rows' => 6
),
'Value',
'/textarea',
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
$expected = array(
'div' => array('class' => 'input date'),
'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
'Created',
'/label',
array('select' => array(
'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
)
),
array('option' => array('value' => '')), '', '/option',
$this->dateRegex['monthsRegex'],
'/select', '-',
array('select' => array(
'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
'id' => 'ValidateProfile1ValidateItem2CreatedDay'
)
),
array('option' => array('value' => '')), '', '/option',
$this->dateRegex['daysRegex'],
'/select', '-',
array('select' => array(
'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
'id' => 'ValidateProfile1ValidateItem2CreatedYear'
)
),
array('option' => array('value' => '')), '', '/option',
$this->dateRegex['yearsRegex'],
'/select',
'/div'
);
$this->assertTags($result, $expected);
$this->Form->validationErrors['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = 'Error';
$this->Form->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
$result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
$expected = array(
'div' => array('class' => 'input text error'),
'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
'Profile',
'/label',
'input' => array(
'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]', 'type' => 'text',
'value' => '1',
'id' => 'ValidateProfile1ValidateItem2ProfileId',
'maxlength' => 8,
'class' => 'form-error'
),
array('div' => array('class' => 'error-message')),
'Error',
'/div',
'/div'
);
$this->assertTags($result, $expected,true);
}
function testMultiRecordFormValidationErrors() {
$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 ce743c0

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