我有一个这样的课:
class Student { const GENDER_MALE = "male",GENDER_FEMALE = "female"; /** * @var string $gender * * @ORM\Column(name="gender",type="string",length=50,nullable=false) * @Assert\NotBlank(message="Gender cannot be blank",groups={"new"}) * @Assert\Choice(choices = {"male","female"},message = "Choose a valid gender.",groups={"new"}) */ private $gender;
我必须硬编码“男”和“女”的价值观.有可能做这样的事情吗?
choices = {self::GENDER_MALE,self::GENDER_FEMALE}
这是
Doctrine2 Annotation Reader (Constants)的一个特点.
原文链接:https://www.f2er.com/php/132172.html你的解决方案
class Student { const GENDER_MALE = "male",GENDER_FEMALE = "female"; /** * @var string $gender * * @ORM\Column(name="gender",nullable=false) * @Assert\NotBlank(message="Gender cannot be blank",groups={"new"}) * @Assert\Choice( * choices = { * Student::GENDER_FEMALE: "Female",* Student::GENDER_MALE: "Male" * },* message = "Choose a valid gender.",groups={"new"} * ) */ private $gender; }