|
| 1 | +Choice |
| 2 | +====== |
| 3 | + |
| 4 | +Validates that a value is one or more of a list of choices. |
| 5 | + |
| 6 | + [yml] |
| 7 | + properties: |
| 8 | + gender: |
| 9 | + - Choice: [male, female] |
| 10 | + |
| 11 | +Options |
| 12 | +------- |
| 13 | + |
| 14 | + * `choices` (**default**, required): The available choices |
| 15 | + * `callback`: Can be used instead of `choices`. A static callback method |
| 16 | + returning the choices. If you pass a string, it is expected to be |
| 17 | + the name of a static method in the validated class. |
| 18 | + * `multiple`: Whether multiple choices are allowed. Default: `false` |
| 19 | + * `min`: The minimum amount of selected choices |
| 20 | + * `max`: The maximum amount of selected choices |
| 21 | + * `message`: The error message if validation fails |
| 22 | + * `minMessage`: The error message if `min` validation fails |
| 23 | + * `maxMessage`: The error message if `max` validation fails |
| 24 | + |
| 25 | +Example 1: Choices as array (YAML) |
| 26 | +---------------------------------- |
| 27 | + |
| 28 | +If the choices are few and easy to determine, they can be passed to the |
| 29 | +constraint definition as array. |
| 30 | + |
| 31 | +**Listing 1** |
| 32 | + |
| 33 | + [yml] |
| 34 | + # Application/HelloBundle/Resources/config/validation.yml |
| 35 | + Application\HelloBundle\Author: |
| 36 | + properties: |
| 37 | + gender: |
| 38 | + - Choice: [male, female] |
| 39 | + |
| 40 | +Example 2: Choices as array (XML) |
| 41 | +--------------------------------- |
| 42 | + |
| 43 | +The following snippet shows the mapping of Example 1 using XML. |
| 44 | + |
| 45 | +**Listing 2** |
| 46 | + |
| 47 | + [xml] |
| 48 | + <!-- Application/HelloBundle/Resources/config/validation.xml --> |
| 49 | + <class name="Application\HelloBundle\Author"> |
| 50 | + <property name="gender"> |
| 51 | + <constraint name="Choice"> |
| 52 | + <value>male</value> |
| 53 | + <value>female</value> |
| 54 | + </constraint> |
| 55 | + </property> |
| 56 | + </class> |
| 57 | + |
| 58 | +Example 3: Choices as array (Docblock Annotations) |
| 59 | +-------------------------------------------------- |
| 60 | + |
| 61 | +Of course, Example 1 can also be solved with annotations. |
| 62 | + |
| 63 | +**Listing 3** |
| 64 | + |
| 65 | + [php] |
| 66 | + // Application/HelloBundle/Author.php |
| 67 | + class Author |
| 68 | + { |
| 69 | + /** |
| 70 | + * @Validation({ @Choice({"male", "female"}) }) |
| 71 | + */ |
| 72 | + protected $gender; |
| 73 | + } |
| 74 | + |
| 75 | +Example 4: Choices from a callback (YAML) |
| 76 | +----------------------------------------- |
| 77 | + |
| 78 | +When you also need the choices in other contexts (such as a drop-down box in |
| 79 | +a form), it is more flexible to bind them to your domain model using a static |
| 80 | +callback method. |
| 81 | + |
| 82 | +**Listing 4** |
| 83 | + |
| 84 | + [php] |
| 85 | + // Application/HelloBundle/Author.php |
| 86 | + class Author |
| 87 | + { |
| 88 | + public static function getGenders() |
| 89 | + { |
| 90 | + return array('male', 'female'); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +You can pass the name of this method to the `callback` option of the `Choice` |
| 95 | +constraint. |
| 96 | + |
| 97 | +**Listing 5** |
| 98 | + |
| 99 | + [yml] |
| 100 | + # Application/HelloBundle/Resources/config/validation.yml |
| 101 | + Application\HelloBundle\Author: |
| 102 | + properties: |
| 103 | + gender: |
| 104 | + - Choice: { callback: getGenders } |
| 105 | + |
| 106 | +If the static callback is stored in a different class, for example `Util`, |
| 107 | +you can pass the class name and the method as array. |
| 108 | + |
| 109 | +**Listing 6** |
| 110 | + |
| 111 | + [yml] |
| 112 | + # Application/HelloBundle/Resources/config/validation.yml |
| 113 | + Application\HelloBundle\Author: |
| 114 | + properties: |
| 115 | + gender: |
| 116 | + - Choice: { callback: [Util, getGenders] } |
| 117 | + |
| 118 | +Example 5: Choices from a callback (XML) |
| 119 | +---------------------------------------- |
| 120 | + |
| 121 | +The following listing shows how Listing 6 is written with XML. |
| 122 | + |
| 123 | +**Listing 7** |
| 124 | + |
| 125 | + [xml] |
| 126 | + <!-- Application/HelloBundle/Resources/config/validation.xml --> |
| 127 | + <class name="Application\HelloBundle\Author"> |
| 128 | + <property name="gender"> |
| 129 | + <constraint name="Choice"> |
| 130 | + <option name="callback"> |
| 131 | + <value>Util</value> |
| 132 | + <value>getGenders</value> |
| 133 | + </option> |
| 134 | + </constraint> |
| 135 | + </property> |
| 136 | + </class> |
| 137 | + |
| 138 | +Example 6: Choices from a callback (Docblock Annotations) |
| 139 | +--------------------------------------------------------- |
| 140 | + |
| 141 | +Here you see how the mapping of Listing 6 is written with annotations. |
| 142 | + |
| 143 | +**Listing 8** |
| 144 | + |
| 145 | + [php] |
| 146 | + // Application/HelloBundle/Author.php |
| 147 | + class Author |
| 148 | + { |
| 149 | + /** |
| 150 | + * @Validation({ @Choice(callback = {"Util", "getGenders"}) }) |
| 151 | + */ |
| 152 | + protected $gender; |
| 153 | + } |
0 commit comments