在 ConfiguredArticle 实体中,您可以使用以下方法:
public function isLengthValid(ExecutionContextInterface $context) {
if ($this->getLength < $this->getArticleConfiguration()->getMinLength()) {
$context->addViolationAt('length', 'The length does not satisfy the minimum length', array(), null);
}
if ($this->getLength > $this->getArticleConfiguration()->getMaxLength()) {
$context->addViolationAt('length', 'The length does not satisfy the maximum length', array(), null);
}
}
public function isWidthValid(ExecutionContextInterface $context) {
if ($this->getWidth < $this->getArticleConfiguration()->getMinWidth()) {
$context->addViolationAt('width', 'The width does not satisfy the minimum width', array(), null);
}
if ($this->getWidth > $this->getArticleConfiguration()->getMaxWidth()) {
$context->addViolationAt('width', 'The width does not satisfy the maximum width', array(), null);
}
}
在此页面上阅读有关如何使用回调方法进行验证的更多信息:http: //symfony.com/doc/current/reference/constraints/Callback.html