svn: /pear/packages/HTML_QuickForm2/trunk/ HTML/QuickForm2/Factory.php HTML/QuickForm2/Rule/Each.php HTML/QuickForm2/Rule.php
tests/QuickForm2/Element/StaticTest.php tests/QuickForm2/Rule/EachTest.php
| From: | Alexey Borzov | Date: | Thu, 30 Sep 2010 20:39:50 +0000 |
| Subject: | svn: /pear/packages/HTML_QuickForm2/trunk/ HTML/QuickForm2/Factory.php HTML/QuickForm2/Rule/Each.php HTML/QuickForm2/Rule.php tests/QuickForm2/Element/StaticTest.php tests/QuickForm2/Rule/EachTest.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-60439@lists.php.net to get a copy of this message | ||
avb Thu, 30 Sep 2010 20:39:50 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=303907
Log:
Prevent validating Static elements (no built-in Rules would work for them anyway)
Changed paths:
U pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Factory.php
U pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Rule/Each.php
U pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Rule.php
U pear/packages/HTML_QuickForm2/trunk/tests/QuickForm2/Element/StaticTest.php
U pear/packages/HTML_QuickForm2/trunk/tests/QuickForm2/Rule/EachTest.php
Modified: pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Factory.php
===================================================================
--- pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Factory.php 2010-09-30 20:37:22 UTC (rev
303906)
+++ pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Factory.php 2010-09-30 20:39:50 UTC (rev
303907)
@@ -81,7 +81,7 @@
'radio' => array('HTML_QuickForm2_Element_InputRadio',
null),
'reset' => array('HTML_QuickForm2_Element_InputReset',
null),
'select' => array('HTML_QuickForm2_Element_Select', null),
- 'static' => array('HTML_QuickForm2_Element_Static'),
+ 'static' => array('HTML_QuickForm2_Element_Static', null),
'submit' => array('HTML_QuickForm2_Element_InputSubmit',
null),
'text' => array('HTML_QuickForm2_Element_InputText', null),
'textarea' => array('HTML_QuickForm2_Element_Textarea', null)
Modified: pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Rule/Each.php
===================================================================
--- pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Rule/Each.php 2010-09-30 20:37:22 UTC (rev
303906)
+++ pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Rule/Each.php 2010-09-30 20:39:50 UTC (rev
303907)
@@ -82,10 +82,12 @@
{
$rule = clone $this->getConfig();
foreach ($this->owner->getRecursiveIterator(RecursiveIteratorIterator::LEAVES_ONLY)
as $child) {
- $rule->setOwner($child);
- if (!$rule->validateOwner()) {
- return false;
- }
+ try {
+ $rule->setOwner($child);
+ if (!$rule->validateOwner()) {
+ return false;
+ }
+ } catch (HTML_QuickForm2_InvalidArgumentException $e) {}
}
return true;
}
@@ -100,8 +102,10 @@
$rule = clone $this->getConfig();
$callbacks = array();
foreach ($this->owner->getRecursiveIterator(RecursiveIteratorIterator::LEAVES_ONLY)
as $child) {
- $rule->setOwner($child);
- $callbacks[] = $rule->getJavascriptCallback();
+ try {
+ $rule->setOwner($child);
+ $callbacks[] = $rule->getJavascriptCallback();
+ } catch (HTML_QuickForm2_InvalidArgumentException $e) {}
}
return "function () { return qf.rules.each([\n\t\t" .
implode(",\n\t\t", $callbacks) . "\n\t]); }";
}
Modified: pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Rule.php
===================================================================
--- pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Rule.php 2010-09-30 20:37:22 UTC (rev
303906)
+++ pear/packages/HTML_QuickForm2/trunk/HTML/QuickForm2/Rule.php 2010-09-30 20:39:50 UTC (rev
303907)
@@ -178,9 +178,19 @@
* Sets the element that will be validated by this rule
*
* @param HTML_QuickForm2_Node Element to validate
+ * @throws HTML_QuickForm2_InvalidArgumentException if trying to set
+ * an instance of HTML_QuickForm2_Element_Static as rule owner
*/
public function setOwner(HTML_QuickForm2_Node $owner)
{
+ // Very little sense to validate static elements as they're, well, static.
+ // If someone comes up with a validation rule for these, he can override
+ // setOwner() there...
+ if ($owner instanceof HTML_QuickForm2_Element_Static) {
+ throw new HTML_QuickForm2_InvalidArgumentException(
+ get_class($this) . ' cannot validate Static elements'
+ );
+ }
if (null !== $this->owner) {
$this->owner->removeRule($this);
}
Modified: pear/packages/HTML_QuickForm2/trunk/tests/QuickForm2/Element/StaticTest.php
===================================================================
--- pear/packages/HTML_QuickForm2/trunk/tests/QuickForm2/Element/StaticTest.php 2010-09-30 20:37:22
UTC (rev 303906)
+++ pear/packages/HTML_QuickForm2/trunk/tests/QuickForm2/Element/StaticTest.php 2010-09-30 20:39:50
UTC (rev 303907)
@@ -104,5 +104,17 @@
$obj->toggleFrozen(true);
$this->assertEquals('<b>content</b>', (string)$obj);
}
+
+ public function testCannotValidate()
+ {
+ $static = new HTML_QuickForm2_Element_Static('novalidate');
+ try {
+ $rule = $this->getMock(
+ 'HTML_QuickForm2_Rule', array('validateOwner'),
+ array($static, 'a message')
+ );
+ $this->fail('Expected HTML_QuickForm2_InvalidArgumentException was not
thrown');
+ } catch (HTML_QuickForm2_InvalidArgumentException $e) { }
+ }
}
?>
\ No newline at end of file
Modified: pear/packages/HTML_QuickForm2/trunk/tests/QuickForm2/Rule/EachTest.php
===================================================================
--- pear/packages/HTML_QuickForm2/trunk/tests/QuickForm2/Rule/EachTest.php 2010-09-30 20:37:22 UTC
(rev 303906)
+++ pear/packages/HTML_QuickForm2/trunk/tests/QuickForm2/Rule/EachTest.php 2010-09-30 20:39:50 UTC
(rev 303907)
@@ -176,4 +176,41 @@
$bar->setValue('');
$this->assertTrue($each->validate());
}
-}
\ No newline at end of file
+
+ public function testIgnoresStaticServerSide()
+ {
+ $mockContainer = $this->getMock(
+ 'HTML_QuickForm2_Container', array('getType', 'setValue',
'__toString')
+ );
+ $mockContainer->addElement('static', 'noValidateServer');
+
+ $rule = $this->getMock(
+ 'HTML_QuickForm2_Rule', array('validateOwner'),
+ array($mockContainer, 'a message')
+ );
+ $rule->expects($this->any())->method('validateOwner')
+ ->will($this->returnValue(false));
+
+ $each = new HTML_QuickForm2_Rule_Each($mockContainer, 'an error', $rule);
+ $this->assertTrue($each->validate());
+ }
+
+ public function testIgnoresStaticClientSide()
+ {
+ $mockContainer = $this->getMock(
+ 'HTML_QuickForm2_Container', array('getType', 'setValue',
'__toString')
+ );
+ $mockContainer->addElement('static', 'noValidateClient');
+
+ $rule = $this->getMock(
+ 'HTML_QuickForm2_Rule', array('validateOwner',
'getJavascriptCallback'),
+ array($mockContainer, 'a message')
+ );
+ $rule->expects($this->any())->method('getJavascriptCallback')
+ ->will($this->returnValue('staticCallback'));
+
+ $each = new HTML_QuickForm2_Rule_Each($mockContainer, 'an error', $rule);
+ $this->assertNotContains('staticCallback', $each->getJavascript());
+ }
+}
+?>
\ No newline at end of file