我有一个与实体PaymentMethod
具有ManyToMany关系的实体发票class Invoice
{
/**
* @ORM\ManyToMany(targetEntity="Evo\BackendBundle\Entity\PaymentMethod")
* @ORM\JoinTable(name="sf_invoices_paymentmethods")
*/
protected $paymentMethods;
}
在我的发票表单类型中,我使用以下脚本添加此字段:
->add('paymentMethods', 'entity', array(
'label' => false,
'class' => 'EvoBackendBundle:PaymentMethod',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('pm')
->where('pm.usedInInvoice = 1')
->orderBy('pm.position');
},
'property' => 'name',
'expanded' => true,
'multiple' => true,
'required' => true,
))
当我查看表单页面生成的源代码时,我可以看到supportMethods复选框中没有required =“required”属性。有谁知道为什么?
如何确保至少检查1个paymentMethod(客户端)并确保每张发票至少有1个paymentMethod(Doctrine side)
我试过@ORM \ JoinTable(name =“sf_invoices_paymentmethods”,nullable = false),但是它会抛出错误。
答案 0 :(得分:2)
找到解决方案。
添加了
'constraints' => array(
new Count(array(
'min' => 1,
'minMessage' => 'At least 1 choice is required',
)),
),
所以表单类型中的字段如下所示:
->add('paymentMethods', 'entity', array(
'label' => false,
'class' => 'EvoBackendBundle:PaymentMethod',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('pm')
->where('pm.usedInInvoice = 1')
->orderBy('pm.position');
},
'property' => 'name',
'expanded' => true,
'multiple' => true,
'constraints' => array(
new Count(array(
'min' => 1,
'minMessage' => 'At least 1 choice is required',
)),
),
))
答案 1 :(得分:-1)
复选框让用户选择有限数量选项的ZERO或MORE选项。这不是关于symfony。这是一个HTML问题,我认为你必须做一些javaScript代码。
对于服务器端,使用validation,在您的情况下choice validation