如何在Symfony中向Doctrine注释添加检查约束

Suppose I have a field gender in my Person entity which can take any of following values

  • MALE
  • FEMALE
  • OTHERS

How can I provide this check using doctrine ORM annotation and then create a corresponding radio button in form using php app/console generate:doctrine:crud?

Can I do that directly? Or do I have to rely on manual approach?

You can specify the valid choices using a Choice constraint as an ORM annotation with your entity property. The Symfony book even uses gender as the example.

In your case the array would be {"MALE", "FEMALE", "OTHER"}, and you may not want that validation error message.

As this validation is in the annotation format you will need to enable annotation validation in your Symfony app configuration (config.yml), I believe this is still disabled by default.