kununu / controller-validation-bundle
Validate controller requests using Symfony validator via annotations
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 6 061
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 17
Forks: 0
Open Issues: 3
Requires
- php: >=5.5.0
- doctrine/annotations: ^1.0
- doctrine/common: ^2.0
- symfony/dependency-injection: ^2.7|^3.0|^4.0
- symfony/http-kernel: ^2.7|^3.0|^4.0
- symfony/validator: ^2.7|^3.0|^4.0
Requires (Dev)
- matthiasnoback/symfony-dependency-injection-test: 0.*
- phpunit/phpunit: ^4.8
- sensio/framework-extra-bundle: ^3.0
- symfony/symfony: ^2.7|^3.0|^4.0
This package is not auto-updated.
Last update: 2021-02-24 04:50:51 UTC
README
This bundle introduces the @Validator annotation, that gives a possibility to validate a Request data using Symfony Validator Constraints and Annotations. Normally Symfony is defining the assertions in the Entities, with this library we can just use a collection constraint for the request data and it'll pick it up automatically thanks to the @Validator annotation.
Installation
Step 1: Install the bundle
composer require kununu/controller-validation-bundle:dev-master
Step 2: Enable the bundle
// config/bundles.php return [ // ... Kununu\ControllerValidationBundle\KununuControllerValidationBundle::class => ['all' => true], ];
Configuration
Constraints namespace
You need to define the namespace where your Constraints are located:
# config/packages/kununu_controller_validation.yml kununu_controller_validation: constraints_namespace: App\Validator\Constraints
Custom Validation Exception
You can define your own Exception to be thrown if needed. Otherwise the default ValidationException will be used:
# app/config/config.yml kununu_controller_validation: validation_exception: class: App\Exception\ValidationException message: Validation error code: 400
Listener priority
The ControllerValidatorListener is by default registered with a priority equal to 0.
In some cases it is needed to dispatch event earlier or later. That's why listener_priority
configuration option was added.
Higher the priority, earlier the event will be dispatched.
# app/config/config.yml kununu_controller_validation: listener_priority: 1
⚠️WARNING: ⚠️ If you're using @ParamConvert annotation, do not change the priority to be less than 0, if you're using any custom ParamConverters. The annotation is going to try to validate the request after the paramConverter is called, which makes no sense if the request is invalid and the actual conversion might fail. If it's absolutely necessary, make sure you manually validate the data in your custom converter, before any other operations.
Note: You can get a list of all registered events using the console command:
bin/console debug:event-dispatcher
Usage
Constraints
<?php // src/Bundle/Validator/Constraints/ExampleConstraints.php namespace App\Validator\Constraints; use Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraints\CollectionValidator; class ExampleConstraints extends Constraints\Collection { public function __construct() { parent::__construct([ 'id' => [ new Constraints\NotBlank(), ], 'name' => new Constraints\NotBlank(), 'example_id' => [ new Constraints\NotBlank(), new Constraints\Range(['min' => 0,'max' => 42]), ], ]); } public function validatedBy() { return CollectionValidator::class; } }
Controller
<?php // src/App/Controller/ExampleController.php namespace App\Controller\ExampleController; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Kununu\ControllerValidationBundle\Annotation\Validator; class ExampleController extends Controller { /** * @param Request $request * * @Validator("ExampleConstraints") */ public function exampleAction(Request $request) { // ... } }