thvvger/symfony-request-validator
最新稳定版本:1.4.9
Composer 安装命令:
composer require thvvger/symfony-request-validator
包简介
Validate symfony request and return JSON response
README 文档
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
FileGeneratorandGenerateClassCommand.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
FileGeneratorservice is configured withautowireandautoconfigureto automatically inject dependencies. - The
GenerateClassCommandis 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
Acknowledgments
统计信息
- 总下载量: 145
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-11-08