0
我有一个表单是一个集合的字段,这个集合必须有固定数量的元素。我如何验证这一点?如何验证表单集合必须具有X个元素?
我有一个表单是一个集合的字段,这个集合必须有固定数量的元素。我如何验证这一点?如何验证表单集合必须具有X个元素?
为拥有该集合的实体编写自定义回调验证程序。
如果你有车实体和产品,例如集合,你应该做的:
...............
use Symfony\Component\Validator\Constraints as Assert;
...............
* @Assert\Callback(
* methods={"hasCorrectNumberOfProducts"}
*)
class Cart
{
...........
public function hasCorrectNumberOfProducts(ExecutionContext $context)
{
$propertyPath = $context->getPropertyPath();
$correct = 666;
if(!count($this->getProducts()) == $correct) {
$context->setPropertyPath($propertyPath . '.products');
$context->addViolation('Incorrect number of products!', array(), null);
}
}
......