20
我正在使用Symfony2和FOSUserBundle。强制一个字段不被要求
正如在文档中详细说明的,我已覆盖并在用户实体中创建了“名称”属性。
我做了所有必要的工作,最后让该字段在窗体视图中显示。
问题是:当我去form_widget(form.name)
并且输入html标签被生成时,在它内部会生成required =“required”属性。这会导致引擎在未填充字段时变为红色。
如何告诉Symfony2不要使该字段为必填字段?我想,那一定是在这里:
parent::buildForm($builder, $options);
// add your custom field
$builder->add('name', 'text', array('label' => 'form.name'));
$builder->remove('username');
或在这里:
/**
* @ORM\Column(type="string", length="255")
*
* @Assert\MinLength(limit="0", message="The name is too short.", groups={"Registration", "Profile"})
* @Assert\MaxLength(limit="255", message="The name is too long.", groups={"Registration", "Profile"})
*/
private $name;
+1 - 也许有补充说,这只是停止表单元素对HTML5验证。所有从你的实体断言等仍然打开,并且必须仍然有效,才能坚持实体。 – insertusernamehere 2012-08-08 16:24:47
有没有办法做到这一点,通过验证组?我似乎无法让它们影响html5所需的属性,这使得验证组对我来说毫无用处。 – 2013-11-04 05:17:43