Created
May 19, 2011 09:27
-
-
Save patashnik/980472 to your computer and use it in GitHub Desktop.
Forms "dynamic" validators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form method="post"> | |
<div> | |
<label>First <input type="radio" name="choice" value="1" /></label> | |
<input type="text" name="first_text" /> | |
</div> | |
<div> | |
<label>Second <input type="radio" name="choice" value="2" /></label> | |
<input type="text" name="second_text" /> | |
</div> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @assert:callback(methods={"isValid"}) | |
*/ | |
class Entity | |
{ | |
public $choice; | |
/** | |
* @var string | |
* | |
* @assert:NotBlank(groups="firstGroup") | |
*/ | |
private $firstText; | |
/** | |
* @var string | |
* | |
* @assert:NotBlank(groups="secondGroup") | |
*/ | |
private $secondText; | |
public function isValid(ExecutionContext $context) | |
{ | |
if ($this->radioButton === '1') { | |
$context | |
->getGraphWalker() | |
->walkReference($this, 'firstGroup', $context->getPropertyPath().'.firstText', true) | |
; | |
} else { | |
$context | |
->getGraphWalker() | |
->walkReference($this, 'secondGroup', $context->getPropertyPath().'.secondText', true) | |
; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment