为什么仍然需要我的选择字段? [英] Why is my choice field still required?

查看:75
本文介绍了为什么仍然需要我的选择字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的联系人实体中有一些字段:

I have some field in my Contact entity:

/**
 * @ORM\Column(type="string", length=6, nullable=true)
 * @Assert\Choice(choices = {"male", "female"})
 */
 protected $gender;

以及用于创建联系人的表单:

And a form for creating contacts:

$builder->add('firstName')
                ->add('lastName')
                ->add('email')
                ->add('gender','choice',array(
                        'choices'   => array('male' => 'male', 'female' => 'female'),
                        'required'  => false,
                    ));

这将导致非必填字段AFAIK. 表格仍然告诉我,我必须选择性别. 有什么想法吗?

This should result in a NOT mandatory field AFAIK. Still the form tells me that I have to select a gender. Any ideas?

推荐答案

您必须使Assert \ Choices注释与可为空的值兼容:

You have to make the Assert\Choices annotation compatible with a nullable value :

/**
 * @ORM\Column(type="string", length=1, nullable=true)
 * @Assert\Choice(choices = {"male", "female", null})
 */
 protected $gender;

这篇关于为什么仍然需要我的选择字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆