Skip to content

Commit

Permalink
Fixing issues with getting values from habtm data after form posting …
Browse files Browse the repository at this point in the history
…and validation has failed.

Tests added.
Refs #279
  • Loading branch information
markstory committed Nov 10, 2009
1 parent adaa2b6 commit f0628d1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cake/libs/view/helper.php
Expand Up @@ -646,7 +646,9 @@ function value($options = array(), $field = null, $key = 'value') {
}

$habtmKey = $this->field();
if (empty($result) && isset($this->data[$habtmKey]) && is_array($this->data[$habtmKey])) {
if (empty($result) && isset($this->data[$habtmKey][$habtmKey])) {
$result = $this->data[$habtmKey][$habtmKey];
} elseif (empty($result) && isset($this->data[$habtmKey]) && is_array($this->data[$habtmKey])) {
if (ClassRegistry::isKeySet($habtmKey)) {
$model =& ClassRegistry::getObject($habtmKey);
$result = $this->__selectedArray($this->data[$habtmKey], $model->primaryKey);
Expand Down
20 changes: 20 additions & 0 deletions cake/tests/cases/libs/view/helper.test.php
Expand Up @@ -367,6 +367,26 @@ function testValue() {
$this->Helper->setEntity('Post.2.created.year');
$result = $this->Helper->value('Post.2.created.year');
$this->assertEqual($result, '2008');

$this->Helper->data = array('HelperTestTag' => array('HelperTestTag' => ''));
$this->Helper->setEntity('HelperTestTag.HelperTestTag');
$result = $this->Helper->value('HelperTestTag.HelperTestTag');
$this->assertEqual($result, '');

$this->Helper->data = array('HelperTestTag' => array('HelperTestTag' => array(2, 3, 4)));
$this->Helper->setEntity('HelperTestTag.HelperTestTag');
$result = $this->Helper->value('HelperTestTag.HelperTestTag');
$this->assertEqual($result, array(2, 3, 4));

$this->Helper->data = array(
'HelperTestTag' => array(
array('id' => 3),
array('id' => 5)
)
);
$this->Helper->setEntity('HelperTestTag.HelperTestTag');
$result = $this->Helper->value('HelperTestTag.HelperTestTag');
$this->assertEqual($result, array(3 => 3, 5 => 5));
}

/**
Expand Down

0 comments on commit f0628d1

Please sign in to comment.