发布于 2015-08-27 16:41:36 | 117 次阅读 | 评论: 0 | 来源: 网络整理
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 |
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:
# src/Acme/BlogBundle/Resources/config/validation.yml
AcmeBlogBundleEntityAuthor:
properties:
gender:
- Choice:
choices: [male, female]
message: Choose a valid gender.
// src/Acme/BlogBundle/Entity/Author.php
namespace AcmeBlogBundleEntity;
use SymfonyComponentValidatorConstraints as Assert;
class Author
{
/**
* @AssertChoice(choices = {"male", "female"}, message = "Choose a valid gender.")
*/
protected $gender;
}
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="AcmeBlogBundleEntityAuthor">
<property name="gender">
<constraint name="Choice">
<option name="choices">
<value>male</value>
<value>female</value>
</option>
<option name="message">Choose a valid gender.</option>
</constraint>
</property>
</class>
</constraint-mapping>
// src/Acme/BlogBundle/EntityAuthor.php
namespace AcmeBlogBundleEntity;
use SymfonyComponentValidatorMappingClassMetadata;
use SymfonyComponentValidatorConstraints as Assert;
class Author
{
protected $gender;
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('gender', new AssertChoice(array(
'choices' => array('male', 'female'),
'message' => 'Choose a valid gender.',
)));
}
}
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 easily access those choices for validation or for building a select form element.
// src/Acme/BlogBundle/Entity/Author.php
namespace AcmeBlogBundleEntity;
class Author
{
public static function getGenders()
{
return array('male', 'female');
}
}
You can pass the name of this method to the callback option of the Choice
constraint.
# src/Acme/BlogBundle/Resources/config/validation.yml
AcmeBlogBundleEntityAuthor:
properties:
gender:
- Choice: { callback: getGenders }
// src/Acme/BlogBundle/Entity/Author.php
namespace AcmeBlogBundleEntity;
use SymfonyComponentValidatorConstraints as Assert;
class Author
{
/**
* @AssertChoice(callback = "getGenders")
*/
protected $gender;
}
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="AcmeBlogBundleEntityAuthor">
<property name="gender">
<constraint name="Choice">
<option name="callback">getGenders</option>
</constraint>
</property>
</class>
</constraint-mapping>
// src/Acme/BlogBundle/EntityAuthor.php
namespace AcmeBlogBundleEntity;
use SymfonyComponentValidatorMappingClassMetadata;
use SymfonyComponentValidatorConstraints as Assert;
class Author
{
protected $gender;
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('gender', new AssertChoice(array(
'callback' => 'getGenders',
)));
}
}
If the static callback is stored in a different class, for example Util
,
you can pass the class name and the method as an array.
# src/Acme/BlogBundle/Resources/config/validation.yml
AcmeBlogBundleEntityAuthor:
properties:
gender:
- Choice: { callback: [Util, getGenders] }
// src/Acme/BlogBundle/Entity/Author.php
namespace AcmeBlogBundleEntity;
use SymfonyComponentValidatorConstraints as Assert;
class Author
{
/**
* @AssertChoice(callback = {"Util", "getGenders"})
*/
protected $gender;
}
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="AcmeBlogBundleEntityAuthor">
<property name="gender">
<constraint name="Choice">
<option name="callback">
<value>Util</value>
<value>getGenders</value>
</option>
</constraint>
</property>
</class>
</constraint-mapping>
// src/Acme/BlogBundle/EntityAuthor.php
namespace AcmeBlogBundleEntity;
use SymfonyComponentValidatorMappingClassMetadata;
use SymfonyComponentValidatorConstraints as Assert;
class Author
{
protected $gender;
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('gender', new AssertChoice(array(
'callback' => array('Util', 'getGenders'),
)));
}
}
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.
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.
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.
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.
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.
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.
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.
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.
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.
type: Boolean
default: false
If true, 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.
type: mixed
default: null
2.6 新版功能: The payload
option was introduced in Symfony 2.6.
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.
For example, you may want to used several error levels to present failed constraint differently in the front-end depending on the severity of the error.