Skip to content

Instantly share code, notes, and snippets.

@patashnik
Created May 19, 2011 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save patashnik/980472 to your computer and use it in GitHub Desktop.
Save patashnik/980472 to your computer and use it in GitHub Desktop.
Forms "dynamic" validators
<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>
<?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