Validation 如何验证表单集合必须包含X个元素?
我有一个带有字段的表单,它是一个集合,这个集合必须有固定数量的元素。如何对此进行验证?为拥有集合的实体编写自定义回调验证器 例如,如果您有购物车实体和产品集合,则应执行以下操作:Validation 如何验证表单集合必须包含X个元素?,validation,symfony,Validation,Symfony,我有一个带有字段的表单,它是一个集合,这个集合必须有固定数量的元素。如何对此进行验证?为拥有集合的实体编写自定义回调验证器 例如,如果您有购物车实体和产品集合,则应执行以下操作: ............... use Symfony\Component\Validator\Constraints as Assert; ............... * @Assert\Callback( * methods={"hasCorrectNumberOfProducts"} * ) clas
...............
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);
}
}
......