-
Notifications
You must be signed in to change notification settings - Fork 3
Home
Validation enhancements for Symfony applications
- Namespace alias configuration
- Constraints
- AtLeastOneOf
- Range
- ServiceCallback
- PHP
5.3.9
or greater -
Doctrine Annotations
1.0
or greater -
Symfony Config component
2.8.0
or greater -
Symfony DependencyInjection component
2.8.0
or greater -
Symfony HttpKernel component
2.8.0
or greater -
Symfony Validator component
2.8.0
or greater
PHPMentorsValidatorBundle
can be installed using Composer.
First, add the dependency to phpmentors/validator-bundle
into your composer.json
file as the following:
Stable version:
composer require phpmentors/validator-bundle "1.3.*"
Development version:
composer require phpmentors/validator-bundle "~1.4@dev"
Second, add PHPMentorsValidatorBundle
into your bundles to register in AppKernel::registerBundles()
as the following:
...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
...
new PHPMentors\ValidatorBundle\PHPMentorsValidatorBundle(),
);
...
This feature allows you to add namespace aliases with configuration. To use this feature add any alias and namespace pair to phpmentors_validator.constraint.namespaces
as the following:
app/config/config.yml
:
...
phpmentors_validator:
constraint:
namespaces:
App: "AppBundle\\Validator\\Constraints\\"
...
The ServiceCallback
constraint and ServiceCallbackValidator
enable to validate a value using a method of a service registered in Symfony's service container.
/**
* @ServiceCallback(service="mydomain.specification.email_limit",method="isSatisfiedBy", message="email limit error.", errorPath="emailMax", groups={"user_check"})
*/
class User
{
/**
* @DI\Service("mydomain.specification.email_limit")
*/
class EmailLimit
{
public function isSatisfiedBy($target)
{
return $target->getEmailsCount() > 5; // Sometimes exceed the limit.
}
}
- When the service method returns
false
, the validation fails. - When any other, the validation passes.
Option | Description |
---|---|
service | Specify service name |
method | Specify method name to be called |
message | (Optional) Error message |
errorPath | (Optional) PropertyPath for which error message displayed in |
git clone https://github.com/phpmentors-jp/validator-bundle.git
If you find a bug or have a question, or want to request a feature, create an issue or pull request for it on Issues.
Copyright (c) 2014-2016, 2018 GOTO Hidenori, KUBO Atsuhiro, and contributors, All rights reserved.