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

时间:2012-05-13 14:52:19

标签: symfony doctrine

我使用this tutorial嵌入了一系列表单。如何添加验证以确保表单至少以一个表单提交?

3 个答案:

答案 0 :(得分:2)

使用callback function,在您的收藏中调用count

答案 1 :(得分:1)

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
    ]),
  ],
]);

答案 2 :(得分:0)

也许您应该在表单类型中创建此表单中的至少一个字段,或者在实体类中作为Assert注释?