Fixing fully-qualified form field references, fixes #4068

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6535 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-03-09 07:36:34 +00:00
parent b1f5b76f98
commit 9a6029f03e
3 changed files with 60 additions and 36 deletions

View file

@ -316,6 +316,8 @@ class Helper extends Overloadable {
if ($setScope) {
$view->modelScope = false;
} elseif (join('.', $view->entity()) == $entity) {
return;
}
if ($entity === null) {
@ -326,11 +328,15 @@ class Helper extends Overloadable {
return;
}
$sameScope = $hasField = false;
$parts = preg_split('/\/|\./', $entity);
$model = $view->model;
$sameScope = $hasField = false;
$parts = array_values(Set::filter(preg_split('/\/|\./', $entity), true));
if(count($parts) === 1 || is_numeric($parts[0])) {
if (empty($parts)) {
return;
}
if (count($parts) === 1 || is_numeric($parts[0])) {
$sameScope = true;
} else {
if (ClassRegistry::isKeySet($parts[0])) {
@ -343,7 +349,7 @@ class Helper extends Overloadable {
for ($i = 0; $i < count($parts); $i++) {
if ($ModelObj->hasField($parts[$i]) || array_key_exists($parts[$i], $ModelObj->validate)) {
$hasField = $i;
if ($hasField === 0) {
if ($hasField === 0 || ($hasField === 1 && is_numeric($parts[0]))) {
$sameScope = true;
}
break;
@ -359,7 +365,6 @@ class Helper extends Overloadable {
array_unshift($parts, $model);
$hasField = true;
}
$view->field = $view->modelId = $view->fieldSuffix = $view->association = null;
switch (count($parts)) {
@ -393,6 +398,13 @@ class Helper extends Overloadable {
list($view->association, $view->field, $view->fieldSuffix) = $parts;
}
break;
case 4:
if ($parts[0] === $view->model) {
list($view->model, $view->modelId, $view->field, $view->fieldSuffix) = $parts;
} else {
list($view->association, $view->modelId, $view->field, $view->fieldSuffix) = $parts;
}
break;
}
if (!isset($view->model) || empty($view->model)) {

View file

@ -187,7 +187,6 @@ class HelperTest extends UnitTestCase {
$this->assertEqual($this->View->entity(), array('HelperTestPost', 5, 'id'));
$this->Helper->setEntity('0.id');
$this->assertTrue($this->View->modelScope);
$this->assertEqual($this->View->model, 'HelperTestPost');

View file

@ -209,6 +209,11 @@ class FormHelperTest extends CakeTestCase {
parent::setUp();
Router::reload();
$this->Form =& new FormHelper();
$this->Form->Html =& new HtmlHelper();
$this->Controller =& new ContactTestController();
$this->View =& new View($this->Controller);
ClassRegistry::addObject('view', $view);
ClassRegistry::addObject('Contact', new Contact());
ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
@ -218,22 +223,6 @@ class FormHelperTest extends CakeTestCase {
ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
}
function startTest($method) {
parent::startTest($method);
$this->Form =& new FormHelper();
$this->Form->Html =& new HtmlHelper();
$this->Controller =& new ContactTestController();
$this->View =& new View($this->Controller);
}
function endTest($method) {
parent::endTest($method);
if (isset($this->Form)) {
unset($this->Form->Html, $this->Form);
}
unset($this->Controller, $this->View);
}
function testFormCreateWithSecurity() {
$this->Form->params['_Token'] = array('key' => 'testKey');
@ -1061,6 +1050,35 @@ class FormHelperTest extends CakeTestCase {
$this->assertPattern('/name="data\[ContactTag\]\[ContactTag\]\[\]"/', $result);
}
function testFormDateTimeMulti() {
$result = $this->Form->dateTime('Contact.1.updated');
$this->assertPattern('/name="data\[Contact\]\[1\]\[updated\]\[day\]"/', $result);
$this->assertPattern('/id="Contact1UpdatedDay"/', $result);
$this->assertPattern('/name="data\[Contact\]\[1\]\[updated\]\[month\]"/', $result);
$this->assertPattern('/id="Contact1UpdatedMonth"/', $result);
$this->assertPattern('/name="data\[Contact\]\[1\]\[updated\]\[year\]"/', $result);
$this->assertPattern('/id="Contact1UpdatedYear"/', $result);
$this->assertPattern('/name="data\[Contact\]\[1\]\[updated\]\[hour\]"/', $result);
$this->assertPattern('/id="Contact1UpdatedHour"/', $result);
$this->assertPattern('/name="data\[Contact\]\[1\]\[updated\]\[min\]"/', $result);
$this->assertPattern('/id="Contact1UpdatedMin"/', $result);
$this->assertPattern('/name="data\[Contact\]\[1\]\[updated\]\[meridian\]"/', $result);
$this->assertPattern('/id="Contact1UpdatedMeridian"/', $result);
$result = $this->Form->dateTime('Contact.2.updated');
$this->assertPattern('/name="data\[Contact\]\[2\]\[updated\]\[day\]"/', $result);
$this->assertPattern('/id="Contact2UpdatedDay"/', $result);
$this->assertPattern('/name="data\[Contact\]\[2\]\[updated\]\[month\]"/', $result);
$this->assertPattern('/id="Contact2UpdatedMonth"/', $result);
$this->assertPattern('/name="data\[Contact\]\[2\]\[updated\]\[year\]"/', $result);
$this->assertPattern('/id="Contact2UpdatedYear"/', $result);
$this->assertPattern('/name="data\[Contact\]\[2\]\[updated\]\[hour\]"/', $result);
$this->assertPattern('/id="Contact2UpdatedHour"/', $result);
$this->assertPattern('/name="data\[Contact\]\[2\]\[updated\]\[min\]"/', $result);
$this->assertPattern('/id="Contact2UpdatedMin"/', $result);
$this->assertPattern('/name="data\[Contact\]\[2\]\[updated\]\[meridian\]"/', $result);
$this->assertPattern('/id="Contact2UpdatedMeridian"/', $result);
}
function testMonth() {
$result = $this->Form->month('Model.field');
@ -1373,10 +1391,7 @@ class FormHelperTest extends CakeTestCase {
'last_name' => 'Abele',
'email' => 'nate@cakephp.org'
));
$this->Form->params = array(
'models' => array('Person'),
'controller' => 'people'
);
$this->Form->params = array('models' => array('Person'), 'controller' => 'people');
$options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
$this->Form->create();
@ -1427,23 +1442,21 @@ class FormHelperTest extends CakeTestCase {
$result = $this->Form->input('Contact.non_existing');
$this->assertPattern('/^<div class="input required">' .
'<label for="ContactNonExisting">Non Existing<\/label>' .
'<input name="data\[Contact\]\[non_existing\]" type="text" value="" id="ContactNonExisting" \/>'.
'<\/div>$/', $result);
'<label for="ContactNonExisting">Non Existing<\/label>' .
'<input name="data\[Contact\]\[non_existing\]" type="text" value="" id="ContactNonExisting" \/>'.
'<\/div>$/', $result);
$result = $this->Form->input('Contact.imnotrequired');
$this->assertPattern('/^<div class="input">' .
'<label for="ContactImnotrequired">Imnotrequired<\/label>' .
'<input name="data\[Contact\]\[imnotrequired\]" type="text" value="" id="ContactImnotrequired" \/>'.
'<\/div>$/', $result);
'<label for="ContactImnotrequired">Imnotrequired<\/label>' .
'<input name="data\[Contact\]\[imnotrequired\]" type="text" value="" id="ContactImnotrequired" \/>'.
'<\/div>$/', $result);
$result = $this->Form->input('Contact.published', array('div' => false));
$this->assertPattern('/^<label for="ContactPublishedMonth">Published<\/label>' .
'<select name="data\[Contact\]\[published\]\[month\]"\s+id="ContactPublishedMonth">/', $result);
'<select name="data\[Contact\]\[published\]\[month\]"\s+id="ContactPublishedMonth">/', $result);
$result = $this->Form->input('Contact.updated', array('div' => false));
$this->assertPattern('/^<label for="ContactUpdatedMonth">Updated<\/label>' .
'<select name="data\[Contact\]\[updated\]\[month\]"\s+id="ContactUpdatedMonth">/', $result);
}
@ -1588,7 +1601,7 @@ class FormHelperTest extends CakeTestCase {
ClassRegistry::removeObject('ValidateItem');
ClassRegistry::removeObject('ValidateUser');
ClassRegistry::removeObject('ValidateProfile');
unset($this->Form);
unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
}
function __sortFields($fields) {