Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/123inkt/symfony-request-validation
Request validation for symfony
https://github.com/123inkt/symfony-request-validation
request symfony symfony-bundle validation
Last synced: 8 days ago
JSON representation
Request validation for symfony
- Host: GitHub
- URL: https://github.com/123inkt/symfony-request-validation
- Owner: 123inkt
- License: mit
- Created: 2020-05-13T15:32:50.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-02-02T10:36:19.000Z (11 months ago)
- Last Synced: 2024-08-09T13:43:52.011Z (5 months ago)
- Topics: request, symfony, symfony-bundle, validation
- Language: PHP
- Size: 113 KB
- Stars: 10
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.1-8892BF)](https://php.net/)
[![Minimum Symfony Version](https://img.shields.io/badge/symfony-%3E%3D%206.2-brightgreen)](https://symfony.com/doc/current/validation.html)
![Run tests](https://github.com/123inkt/symfony-request-validation/actions/workflows/test.yml/badge.svg)# Symfony Request Validation
A request validation component for Symfony. Ease the validation of request properties without the need for an entire Symfony Form.## Installation
Include the library as dependency in your own project via:
```bash
composer require "digitalrevolution/symfony-request-validation"
```Update `/config/bundles.php`:
```php
return [
...
DigitalRevolution\SymfonyRequestValidation\Bundle\RequestValidationBundle::class => ['all' => true],
];
```## Usage
1) Create your own `ExampleRequest` class which extends the `AbstractValidatedRequest` class.
2) Configure your own `ValidationRules`. See the [Validation shorthand library](https://github.com/123inkt/symfony-validation-shorthand) for
more information about the rules.
3) Ensure your `ExampleRequest` class is registered as [service in your Symfony project](https://symfony.com/doc/current/service_container.html).```php
use DigitalRevolution\SymfonyRequestValidation\AbstractValidatedRequest;
use DigitalRevolution\SymfonyRequestValidation\ValidationRules;class ExampleRequest extends AbstractValidatedRequest
{
protected function getValidationRules(): ValidationRules
{
return new ValidationRules([
'request' => [
'productId' => 'required|int|min:0',
'productName' => 'required|string|between:50,255'
]
]);
}public function getProductId(): int
{
return $this->request->request->getInt('productId');
}
public function getProductName(): string
{
return $this->request->request->getString('productName');
}
}
```All that remains is using your `ExampleRequest` class in your `Controller` and it will only be invoked when the request validation passes.
```php
class ExampleController
{
/**
* @Route("/", name="my_example")
*/
public function index(ExampleRequest $request): Response
{
return ...;
}
}
```## Invalid request handling
By default if a request is invalid an `InvalidRequestException` will be thrown. If you prefer a different behaviour, overwrite the `handleViolations`
method.
```php
class ExampleRequest extends AbstractValidatedRequest
{
...
protected function handleViolations(ConstraintViolationListInterface $violationList): void
{
$renderer = new ViolationListRenderer($violationList);
$this->logger->error($renderer->render());
}
}
```Note: if no exceptions are thrown in the `handleViolations`, you'll always receive a request in your `Controller`. Use `Request->isValid()` to verify
the request is valid.## About us
At 123inkt (Part of Digital Revolution B.V.), every day more than 50 development professionals are working on improving our internal ERP
and our several shops. Do you want to join us? [We are looking for developers](https://www.werkenbij123inkt.nl/zoek-op-afdeling/it).