thvvger / symfony-request-validator
Validate symfony request and return JSON response
Installs: 133
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- jawira/case-converter: ^3.5
- symfony/console: >=6.1
- symfony/framework-bundle: >=6.1
- symfony/http-foundation: >=6.0
- symfony/validator: >=6.0
Requires (Dev)
- symfony/console: >=6.1
README
Table of Contents
Symfony Request Validator
RequestValidator
is a Symfony bundle that simplifies the process of validating request data and generating request classes with validation rules. It integrates with Symfony's Validator component and provides an easy way to generate request classes with built-in validation constraints.
Getting Started
This is an example of how you may give instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.
Installation
- Install the package
composer require thvvger/symfony-request-validator
-
Configure the Services : After installing the bundle, you need to register the services for the
FileGenerator
andGenerateClassCommand
.Add the following configuration to your
config/services.yaml
:services: #.... # Register FileGenerator service Thvvger\RequestValidator\Services\FileGenerator: autowire: true autoconfigure: true arguments: $projectDir: '%kernel.project_dir%' # Inject the project directory path # Register GenerateClassCommand as a console command Thvvger\RequestValidator\Command\GenerateClassCommand: autowire: true autoconfigure: true arguments: $fileGenerator: '@Thvvger\RequestValidator\Services\FileGenerator' # Inject the FileGenerator service tags: - { name: 'console.command' } # Register the command for Symfony's console
- The
FileGenerator
service is configured withautowire
andautoconfigure
to automatically inject dependencies. - The
GenerateClassCommand
is registered as a console command, allowing you to execute it from the command line usingphp bin/console generate:class
.
- The
-
Configuration
Ensure that the bundle is registered in `config/bundles.php:
return [ Thvvger\RequestValidator\RequestValidatorBundle::class => ['all' => true], ];
Usage
Generate a Request Class
To generate a request class, use the following command:
php bin/console make:request TestRequest
This will generate a PHP file in the src/Request/ directory with a base structure for your request class.
Example Generated Class
Here’s an example of a class generated after running the command:
namespace App\Request; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\Type; use Thvvger\RequestValidator\BaseRequest; class TestRequest extends BaseRequest { #[NotBlank] #[Type('string')] public readonly string $name; // add other properties }
Example Usage in Your Controller
After generating the TestRequest
class with the relevant validation logic, you can use it within a controller to handle incoming requests, perform validation, and execute any necessary logic (such as file generation).
#[Route('/test', methods: ['POST'])] public function test(TestRequest $request): JsonResponse { //... // Return a success message after the request is processed return new JsonResponse([ 'message' => 'Request exécuted succesfully', ]); }
If validation fails (e.g., missing or invalid file), a response like the following will be returned:
{ "message": "Validation error", "errors": { "fichier": "This value is invalid", "someProperty": "This value cannot be blank." } }
Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
License
Distributed under the MIT License. See LICENSE.txt
for more information.
Contact
Larry-Bill ADJE - @twitter_handle - thvger@gmail.com