Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Disallow server-side error messages for hidden elements

git-svn-id: http://svn.php.net/repository/pear/packages/HTML_QuickForm2/trunk@325773 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
commit f5adcdd538e5feb3cf3a7f62e40f33236c0a0482 1 parent 97e4853
Alexey Borzov sad-spirit authored
18 HTML/QuickForm2/Element/InputHidden.php
@@ -76,6 +76,24 @@ public function toggleFrozen($freeze = null)
76 76 return false;
77 77 }
78 78
  79 + /**
  80 + * Disallows setting an error message on hidden elements
  81 + *
  82 + * @param string|null $error
  83 + *
  84 + * @return HTML_QuickForm2_Element_InputHidden
  85 + * @throws HTML_QuickForm2_InvalidArgumentException if $error is not empty
  86 + */
  87 + public function setError($error = null)
  88 + {
  89 + if (strlen($error)) {
  90 + throw new HTML_QuickForm2_InvalidArgumentException(
  91 + "Hidden elements cannot have validation errors"
  92 + );
  93 + }
  94 + return parent::setError($error);
  95 + }
  96 +
79 97 public function render(HTML_QuickForm2_Renderer $renderer)
80 98 {
81 99 $renderer->renderHidden($this);
24 HTML/QuickForm2/Rule.php
@@ -180,9 +180,19 @@ public function getConfig()
180 180 * @param string $message Error message to display if validation fails
181 181 *
182 182 * @return HTML_QuickForm2_Rule
  183 + * @throws HTML_QuickForm2_InvalidArgumentException if trying to validate
  184 + * HTML_QuickForm2_Element_InputHidden with a non-empty error message
  185 + * (e.g. not in Rule chain)
183 186 */
184 187 public function setMessage($message)
185 188 {
  189 + if ($this->owner instanceof HTML_QuickForm2_Element_InputHidden
  190 + && strlen($message)
  191 + ) {
  192 + throw new HTML_QuickForm2_InvalidArgumentException(
  193 + "Hidden elements cannot have validation errors"
  194 + );
  195 + }
186 196 $this->message = (string)$message;
187 197 return $this;
188 198 }
@@ -203,7 +213,9 @@ public function getMessage()
203 213 * @param HTML_QuickForm2_Node $owner Element to validate
204 214 *
205 215 * @throws HTML_QuickForm2_InvalidArgumentException if trying to set
206   - * an instance of HTML_QuickForm2_Element_Static as rule owner
  216 + * an instance of HTML_QuickForm2_Element_Static as rule owner; if
  217 + * trying to validate HTML_QuickForm2_Element_InputHidden with a
  218 + * non-empty error message (e.g. not in Rule chain)
207 219 */
208 220 public function setOwner(HTML_QuickForm2_Node $owner)
209 221 {
@@ -215,6 +227,13 @@ public function setOwner(HTML_QuickForm2_Node $owner)
215 227 get_class($this) . ' cannot validate Static elements'
216 228 );
217 229 }
  230 + if ($owner instanceof HTML_QuickForm2_Element_InputHidden
  231 + && strlen($this->getMessage())
  232 + ) {
  233 + throw new HTML_QuickForm2_InvalidArgumentException(
  234 + "Hidden elements cannot have validation errors"
  235 + );
  236 + }
218 237 if (null !== $this->owner) {
219 238 $this->owner->removeRule($this);
220 239 }
@@ -282,6 +301,7 @@ public function validate()
282 301 $globalValid = false;
283 302 $localValid = $this->validateOwner();
284 303 foreach ($this->chainedRules as $item) {
  304 + /* @var $multiplier HTML_QuickForm2_Rule */
285 305 foreach ($item as $multiplier) {
286 306 if (!($localValid = $localValid && $multiplier->validate())) {
287 307 break;
@@ -354,6 +374,7 @@ protected function getJavascriptTriggers()
354 374 {
355 375 $triggers = array_flip($this->getOwnJavascriptTriggers());
356 376 foreach ($this->chainedRules as $item) {
  377 + /* @var $multiplier HTML_QuickForm2_Rule */
357 378 foreach ($item as $multiplier) {
358 379 foreach ($multiplier->getJavascriptTriggers() as $trigger) {
359 380 $triggers[$trigger] = true;
@@ -396,6 +417,7 @@ public function getJavascript($outputTriggers = true)
396 417 $chained = array();
397 418 foreach ($this->chainedRules as $item) {
398 419 $multipliers = array();
  420 + /* @var $multiplier HTML_QuickForm2_Rule */
399 421 foreach ($item as $multiplier) {
400 422 $multipliers[] = $multiplier->getJavascript(false);
401 423 }
9 tests/QuickForm2/Element/InputHiddenTest.php
@@ -56,5 +56,14 @@ public function testCannotBeFrozen()
56 56 $this->assertFalse($hidden->toggleFrozen(true));
57 57 $this->assertFalse($hidden->toggleFrozen());
58 58 }
  59 +
  60 + /**
  61 + * @expectedException HTML_QuickForm2_InvalidArgumentException
  62 + */
  63 + public function testCannotSetError()
  64 + {
  65 + $hidden = new HTML_QuickForm2_Element_InputHidden('noError');
  66 + $hidden->setError('a message');
  67 + }
59 68 }
60 69 ?>
23 tests/QuickForm2/RuleTest.php
@@ -241,5 +241,28 @@ public function testChainedValidationTriggers()
241 241 $this->assertContains('bar', $m[0]);
242 242 $this->assertContains('baz', $m[0]);
243 243 }
  244 +
  245 + public function testCannotSetErrorsOnHiddenElements()
  246 + {
  247 + $hidden = new HTML_QuickForm2_Element_InputHidden('noError');
  248 + $text = new HTML_QuickForm2_Element_InputText('canHaveError');
  249 +
  250 + try {
  251 + $rule = $this->getMock(
  252 + 'HTML_QuickForm2_Rule', array('validateOwner'),
  253 + array($hidden, 'an error message')
  254 + );
  255 + $this->fail('Expected HTML_QuickForm2_InvalidArgumentException was not thrown');
  256 + } catch (HTML_QuickForm2_InvalidArgumentException $e) {}
  257 +
  258 + try {
  259 + $rule = $this->getMock(
  260 + 'HTML_QuickForm2_Rule', array('validateOwner'),
  261 + array($text, 'an error message')
  262 + );
  263 + $rule->setOwner($hidden);
  264 + $this->fail('Expected HTML_QuickForm2_InvalidArgumentException was not thrown');
  265 + } catch (HTML_QuickForm2_InvalidArgumentException $e) {}
  266 + }
244 267 }
245 268 ?>

0 comments on commit f5adcdd

Please sign in to comment.
Something went wrong with that request. Please try again.