私は 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
ます...基本的にはすべて同じです...
お役に立てれば....