2014-12-08 31 views
-1

谁能告诉我,为什么在形式需要我有一个领域:为什么Symfony2的形式要求fiels

<input type="checkbox" id="client_invoice" name="client[invoice]" class="invoice-controller" value="1" required="required"> 

如果实体我设置:

/** 
* @ORM\Column(type="boolean", nullable=true) 
* 
* @var boolean 
*/ 
protected $invoice; 

我客串是becouse在我有:

$builder->add('invoice', 'checkbox', array('label' => 'form.client.invoice')); 

然后'必需的'值被自动设置为true(因为add函数中的第三个参数)。我是对的,还是有另一个理由,这个领域是必需的?

+0

http://symfony.com/doc/current/book/forms.html#field-type-options-guessing – JayKey 2014-12-08 08:47:44

+1

*“这些领域的选择是:您可以将其关闭指定的表单字段只有当你使用Symfony来猜测字段类型时(如**省略或者传递null **作为第二个参数来添加()),才猜到了。“*你正在传递'checkbox'。 (但很好的电话;)) – Yoshi 2014-12-08 08:50:00

+0

这就是我正在寻找的答案,谢谢:) – JayKey 2014-12-08 08:50:57

回答

0

在Symfony2中默认情况下需要表单字段。

$builder->add('invoice', 'checkbox', array(
    'label' => 'form.client.invoice', 
    'required' => false, 
)); 
+0

我知道我可以设置为false。如果您在实体中设置了nullable = true,则归档会自动设置为'required'=> false。 – JayKey 2014-12-08 08:04:23

+0

是的。如果您使用复选框,可能会很烦人,但在其他情况下,这是一个很好的功能。 – 2014-12-08 08:14:59

+0

如果在实体中设置为空,则不需要提交。我想知道为什么如果我将它们设为空时它们被设置为在生成表单时需要。 – JayKey 2014-12-08 08:29:29