Download the PHP package mojtaba-gheytasi/request-validator-bundle without Composer
On this page you can find all versions of the php package mojtaba-gheytasi/request-validator-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mojtaba-gheytasi/request-validator-bundle
More information about mojtaba-gheytasi/request-validator-bundle
Files in mojtaba-gheytasi/request-validator-bundle
Package request-validator-bundle
Short Description Symfony bundle for validating request parameters in a clean structure
License MIT
Homepage https://github.com/mojtaba-gheytasi/request-validator-bundle
Informations about the package request-validator-bundle
Symfony Request Validator Bundle
What does it do? :)
This bundle allows you to validate request parameters based on your constraints and restrictions via request classes that contain validation logic.
Installation
Compatibility
- PHP v7.4 or above
- Symfony v4.4 or above
Usage
Suppose you want to validate incoming request parameters as follows:
To start, create a request class by following command:
NOTE: Creates SearchProductRequest.php in src/Request.
Now add your validation constraints to the constraints method:
So, how are the validation constraints evaluated? All you need to do is type-hint the request on your controller method. The incoming request data is validated before the controller method is called, meaning you do not need to clutter your controller with validation logic:
If validate incoming request parameters as follows, can see error messages:
Suggestion
It's better to prevent duplicate below code in your controller methods,
RequestValidatorBundle provides a good solution for doing this, first create a class that implements MojtabaGheytasi\RequestValidatorBundle\Contract\FailedValidationInterface and writes your logic in the onFailedValidation method:
Now just make a yaml file in config/packages/ directory and define the above class to RequestValidatorBundle like the following:
After doing these, when the request has an error, RequestValidatorBundle immediately executes the onFailedValidation method, and if you throw an exception or anyway make a response in the onFailedValidation method, the controller method codes don't execute. so you can write controller methods like below:
Contributing
If you find an issue, or have a better way to do something, feel free to open an issue or a pull request.
All versions of request-validator-bundle with dependencies
symfony/framework-bundle Version ^4.4 || ^5.0
symfony/validator Version ^4.4 || ^5.0