У меня такой класс:
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;
Мне приходится жестко кодировать значения "male"
и "female"
. Можно ли сделать что-то подобное?
choice = {self :: GENDER_MALE, self :: GENDER_FEMALE}
Это особенность Doctrine2 Annotation Reader (Константы) .
Решение:
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 = { * Student::GENDER_FEMALE: "Female", * Student::GENDER_MALE: "Male" * }, * message = "Choose a valid gender.", groups={"new"} * ) */ private $gender; }