我会去CallbackValidation:
http://symfony.com/doc/2.0/reference/constraints/Callback.html
在通话期间会触发验证,isValid()
因此您将知道它何时失败。
至于验证代码,我认为这应该做的事情(我还没有运行它):
/**
* @Assert\Callback(methods={"isYearWeekValid"})
*/
class YourEntity
{
... here go $year and $week
public function isYearWeekValid(ExecutionContext $context)
{
$firstInYear = \DateTime:createFromFormat('Y-m-d', $this->year . '-01-01');
$interval = new \DateInterval(sprintf('P%dW', $this->week * 7));
$firstDayOfWeek = $firstInYear->add($interval); # beware, $firstInYear object was modified hear as well
$now = new \DateTime();
if ( $firstInWeek < $now ){
$context->addViolation('Invalid year/week combination!', array(), null);
}else{
// IT'S OK
}
}
}
我使用注释来指定验证,但为此您也可以使用XML
or YAML
... 基本上都是一样的...
希望这可以帮助....