Choice
Warning: You are browsing the documentation for Symfony 3.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.1 (the current stable version).
This constraint is used to ensure that the given value is one of a given set of valid choices. It can also be used to validate that each item in an array of items is one of those valid choices.
Applies to | property or method |
Options | |
Class | Choice |
Validator | ChoiceValidator |
Basic Usage
The basic idea of this constraint is that you supply it with an array of valid values (this can be done in several ways) and it validates that the value of the given property exists in that array.
If your valid choice list is simple, you can pass them in directly via the choices option:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Author
{
const GENRES = ['fiction', 'non-fiction'];
/**
* @Assert\Choice({"New York", "Berlin", "Tokyo"})
*/
protected $city;
/**
* You can also directly provide an array constant to the "choices" option in the annotation
*
* @Assert\Choice(choices=Author::GENRES, message="Choose a valid genre.")
*/
protected $genre;
}
Supplying the Choices with a Callback Function
You can also use a callback function to specify your options. This is useful if you want to keep your choices in some central location so that, for example, you can access those choices for validation or for building a select form element:
1 2 3 4 5 6 7 8 9 10
// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;
class Author
{
public static function getGenres()
{
return ['fiction', 'non-fiction'];
}
}
3.2
As of Symfony 3.2 the callback no longer needs to be static.
You can pass the name of this method to the callback option of the Choice
constraint.
1 2 3 4 5 6 7 8 9 10 11 12
// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Author
{
/**
* @Assert\Choice(callback="getGenres")
*/
protected $genre;
}
If the callback is defined in a different class and is static, for example AppBundle\Entity\Genre
,
you can pass the class name and the method as an array.
1 2 3 4 5 6 7 8 9 10 11 12
// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Author
{
/**
* @Assert\Choice(callback={"AppBundle\Entity\Genre", "getGenres"})
*/
protected $genre;
}
Available Options
choices
type: array
[default option]
A required option (unless callback is specified) - this is the array of options that should be considered in the valid set. The input value will be matched against this array.
callback
type: string|array|Closure
This is a callback method that can be used instead of the choices option to return the choices array. See Supplying the Choices with a Callback Function for details on its usage.
groups
type: array
| string
It defines the validation group or groups this constraint belongs to. Read more about validation groups.
multiple
type: boolean
default: false
If this option is true, the input value is expected to be an array instead of a single, scalar value. The constraint will check that each value of the input array can be found in the array of valid choices. If even one of the input values cannot be found, the validation will fail.
min
type: integer
If the multiple
option is true, then you can use the min
option
to force at least XX number of values to be selected. For example, if
min
is 3, but the input array only contains 2 valid items, the validation
will fail.
max
type: integer
If the multiple
option is true, then you can use the max
option
to force no more than XX number of values to be selected. For example, if
max
is 3, but the input array contains 4 valid items, the validation
will fail.
message
type: string
default: The value you selected is not a valid choice.
This is the message that you will receive if the multiple
option is
set to false
and the underlying value is not in the valid array of
choices.
You can use the following parameters in this message:
Parameter | Description |
---|---|
{{ value }} |
The current (invalid) value |
multipleMessage
type: string
default: One or more of the given values is invalid.
This is the message that you will receive if the multiple
option is
set to true
and one of the values on the underlying array being checked
is not in the array of valid choices.
You can use the following parameters in this message:
Parameter | Description |
---|---|
{{ value }} |
The current (invalid) value |
minMessage
type: string
default: You must select at least {{ limit }} choices.
This is the validation error message that's displayed when the user chooses too few choices per the min option.
You can use the following parameters in this message:
Parameter | Description |
---|---|
{{ limit }} |
The lower limit of choices |
maxMessage
type: string
default: You must select at most {{ limit }} choices.
This is the validation error message that's displayed when the user chooses too many options per the max option.
You can use the following parameters in this message:
Parameter | Description |
---|---|
{{ limit }} |
The upper limit of choices |
strict
type: boolean
default: false
The validator will also check the type of the input value. Specifically, this value is passed to as the third argument to the PHP in_array method when checking to see if a value is in the valid choices array.
Caution
Setting the strict option of the Choice Constraint to false
has been
deprecated as of Symfony 3.2 and the option will be changed to true
as of 4.0.
payload
type: mixed
default: null
This option can be used to attach arbitrary domain-specific data to a constraint. The configured payload is not used by the Validator component, but its processing is completely up to you.
For example, you may want to use several error levels to present failed constraints differently in the front-end depending on the severity of the error.