Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use spl_object_hash() instead of element ids as key for storing valid…

…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...
commit f1ab56a681be975934a83df4e3c4c6367f20b913 1 parent e444c7e
@sad-spirit sad-spirit authored
View
9 HTML/QuickForm2/Container/Repeat.php
@@ -424,6 +424,7 @@ public function setIndexes(array $indexes)
* deduce indexes taken by repeat items.
*
* @see setIndexField()
+ * @throws HTML_QuickForm2_Exception
*/
protected function updateValue()
{
@@ -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;
}
}
}
@@ -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);
View
31 tests/QuickForm2/Container/RepeatTest.php
@@ -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']);
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.