使用 symfony2 的表单类型“集合”时,如何验证至少已提交一个表单?

mat*_*ndr 3 doctrine symfony

我使用本教程嵌入了一组表单。如何添加验证以确保提交的表单至少包含一个表单?

chr*_*guy 6

Symfony 现在有一个Count约束,可以与集合类型一起使用来设置最小项目数:

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

$formBuilder->add('example', CollectionType::class, [
  // other options...
  'constraints' => [
    new Assert\Count([
      'min' => 1,
      'minMessage' => 'Must have at least one value',
      // also has max and maxMessage just like the Length constraint
    ]),
  ],
]);
Run Code Online (Sandbox Code Playgroud)


i.a*_*iel 2

使用回调函数,对您的集合进行调用计数