I have a class like this:
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;
I have to hard code the values "male"
and "female"
. Is it possible to do something like this ?
choices = {self::GENDER_MALE, self::GENDER_FEMALE}
This is a feature of Doctrine2 Annotation Reader (Constants).
You solution:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With