我使用this tutorial嵌入了一系列表单。如何添加验证以确保表单至少以一个表单提交?
答案 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注释?