Skip to content

Commit

Permalink
Use spl_object_hash() instead of element ids as key for storing valid…
Browse files Browse the repository at this point in the history
…ation errors of repeated elements, errors now work properly (and even survive id changes).

git-svn-id: http://svn.php.net/repository/pear/packages/HTML_QuickForm2/trunk@325689 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
sad-spirit committed May 14, 2012
1 parent e444c7e commit f1ab56a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
9 changes: 6 additions & 3 deletions HTML/QuickForm2/Container/Repeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ public function setIndexes(array $indexes)
* deduce indexes taken by repeat items.
*
* @see setIndexField()
* @throws HTML_QuickForm2_Exception
*/
protected function updateValue()
{
Expand Down Expand Up @@ -626,7 +627,7 @@ protected function validate()
/* @var HTML_QuickForm2_Node $child */
foreach ($this->getRecursiveIterator() as $child) {
if (strlen($error = $child->getError())) {
$this->childErrors[$child->getId()] = $error;
$this->childErrors[spl_object_hash($child)][$index] = $error;
}
}
}
Expand Down Expand Up @@ -699,8 +700,10 @@ public function render(HTML_QuickForm2_Renderer $renderer)
$this->replaceIndexTemplates($index, $backup);
/* @var HTML_QuickForm2_Node $child */
foreach ($this->getRecursiveIterator() as $child) {
if (isset($this->childErrors[$id = $child->getId()])) {
$child->setError($this->childErrors[$id]);
if (isset($this->childErrors[$hash = spl_object_hash($child)])
&& isset($this->childErrors[$hash][$index])
) {
$child->setError($this->childErrors[$hash][$index]);
}
}
$this->getPrototype()->render($renderer);
Expand Down
31 changes: 31 additions & 0 deletions tests/QuickForm2/Container/RepeatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,36 @@ public function testFrozenRepeatShouldNotContainJavascript()

$this->assertNotContains('<script', $repeat->__toString());
}

public function testServerSideValidationErrors()
{
$ds = new HTML_QuickForm2_DataSource_Session(array(
'foo' => array('', 'blah', '')
));
$form = new HTML_QuickForm2('repeatValidate');
$form->addDataSource($ds);

$fieldset = new HTML_QuickForm2_Container_Fieldset();
$text = new HTML_QuickForm2_Element_InputText('foo');
$repeat = new HTML_QuickForm2_Container_Repeat(
null, null, array('prototype' => $fieldset)
);
$fieldset->appendChild($text);
$form->appendChild($repeat);

$text->addRule('required', 'a message');
$this->assertFalse($form->validate());

$ary = $repeat->render(HTML_QuickForm2_Renderer::factory('array'))->toArray();
$this->assertEquals('a message', $ary['elements'][1]['elements'][0]['error']);
$this->assertArrayNotHasKey('error', $ary['elements'][2]['elements'][0]);
$this->assertEquals('a message', $ary['elements'][3]['elements'][0]['error']);

$text->setId('blah-:idx:');
$ary = $repeat->render(HTML_QuickForm2_Renderer::factory('array'))->toArray();
$this->assertEquals('a message', $ary['elements'][1]['elements'][0]['error']);
$this->assertArrayNotHasKey('error', $ary['elements'][2]['elements'][0]);
$this->assertEquals('a message', $ary['elements'][3]['elements'][0]['error']);
}
}
?>

0 comments on commit f1ab56a

Please sign in to comment.