Force a field to not be required

大兔子大兔子 提交于 2019-12-30 03:43:48

问题


I am using Symfony2 and FOSUserBundle.

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.

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.

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');

or here:

    /**
 * @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:


$builder->add('name', 'text', array('label' => 'form.name','required' => false));



回答2:


use Symfony\Component\Validator\Constraints\NotNull;

$builder->add('name', 'text', array('label' => 'form.name', 'constraints' => new NotNull()));



来源:https://stackoverflow.com/questions/11868497/force-a-field-to-not-be-required

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!