Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.