Skip to content
Atsuhiro Kubo edited this page May 22, 2018 · 5 revisions

PHPMentorsValidatorBundle

Validation enhancements for Symfony applications

Total Downloads Latest Stable Version Latest Unstable Version

Features

  • Namespace alias configuration
  • Constraints
    • AtLeastOneOf
    • Range
    • ServiceCallback

Requirements

Installation

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(),
        );
        ...

Documentation

Namespace alias configuration

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\\"
...

ServiceCallback constraint

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.
    }
}

Return value and validation result

  • When the service method returns false, the validation fails.
  • When any other, the validation passes.

Options

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

Source

Browse Source Code

Browse Source Code

Git Access

git clone https://github.com/phpmentors-jp/validator-bundle.git

Support

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

Copyright (c) 2014-2016, 2018 GOTO Hidenori, KUBO Atsuhiro, and contributors, All rights reserved.

License

The BSD 2-Clause License