强制不需要字段 [英] Force a field to not be required
问题描述
我正在使用Symfony2和FOSUserBundle.
I am using Symfony2 and FOSUserBundle.
就像文档中所详述的那样,我已经覆盖并在User实体中创建了"name"属性.
Just as detailed in the documentation, I have overridden and created a "name" property in the User entity.
我做了所有必要的工作,最后使该字段显示在表单视图中.
I do all necessary and finally get that field to be shown in the form view.
问题是:当我进入form_widget(form.name)
并生成输入html标记时,将在其中生成required ="required"属性.当字段未填写时,这会导致引擎将输入变为红色.
The thing is: when I go form_widget(form.name)
and the input html tag is generated, a required="required" property is generated within it. And that causes the engine to red the input when the field is not filled in.
如何告诉Symfony2不要将该字段设为必选字段?我想它必须在这里:
How do I do to tell the Symfony2 not to make that field mandatory? I guess that it has to be here:
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;
推荐答案
$builder->add('name', 'text', array('label' => 'form.name','required' => false));
这篇关于强制不需要字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!