sharifi / symfony-validation-bundle
Symfony SymfonyValidationBundle
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 4
Forks: 1
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^7.4
- symfony/framework-bundle: ^4.0|^5.0
- symfony/validator: 4.0|^5.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ^7.1|^8.0
- sensio/framework-extra-bundle: ^5.5
- symfony/browser-kit: ^5.0
This package is auto-updated.
Last update: 2024-10-09 20:48:41 UTC
README
Introduction
This package allows you to validate request parameters based on your rules and restrictions via form requests. Form requests are custom request classes that contain validation logic.
Installation
You can install the package via composer:
composer require sharifi/symfony-validation-bundle
Creating Form Requests
To start, create a class which extends from Sharifi\Bundle\SymfonyValidationBundle\Requests\FormRequest
and add your validation rules to the rules method:
use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\NotBlank; use Sharifi\Bundle\SymfonyValidationBundle\Requests\FormRequest; class StoreBlogPost extends FormRequest { /** * Get the validation rules that apply to the request. */ protected function rules(): array { return [ 'name' => [ new Length(['max' => 255]), new NotBlank(), ], ]; } }
Use a Form Request
So, how are the validation rules evaluated? All you need to do is type-hint the request on your controller method. The incoming form request is validated before the controller method is called, meaning you do not need to clutter your controller with any validation logic:
/** * Store the incoming blog post. */ public function store(StoreBlogPost $request): Response { // The incoming request is valid... // Retrieve the validated input data... $validated = $request->validated(); }