youniverse-center / request-validation-bundle
Symfony Bundle for automatic validation of data provided in the request.
Installs: 29
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.0
- symfony/config: ^5.3
- symfony/dependency-injection: ^5.3
- symfony/event-dispatcher: ^5.3
- symfony/http-kernel: ^5.3
- symfony/validator: ^5.3
This package is auto-updated.
Last update: 2024-12-20 21:50:17 UTC
README
- Add validator class implementing
Yc\RequestValidationBundle\RequestValidator\RequestValidatorInterface
- in the
getConstriant
return constraints used by the validator component - in the
getGroups
return validation groups getInvalidRequestResponse
must return response that will be used if the validation has failed.
- Add attribute
Yc\RequestValidationBundle\Attributes\RequestValidator
to your controller
#[Route('/some/route', name: 'some_route')] #[RequestValidator(Create::class)] class CreateController extends AbstractController { public function __invoke($data) { // in the data is your validated request content } }
(This attribute can be also placed on a method if you have multple controllers in a class.)
- You probably want to receive data from the request for the validation in your specific way. For this purpose implement
Yc\RequestValidationBundle\DataReceiver\DataReceiverInterface
for example:
public function getData(Request $request): mixed { return json_decode($request->getContent(), true); }
- By default the data will be set in the request attribute
data
if you want to change this, implementYc\RequestValidationBundle\DataTransformer\DataTransformerInterface
for example:
public function transformData(mixed $data): array { $id = ProjectId::fromString($data['id']); return [ 'project' => new Project($id, $data['name']) ]; }
and then you can use it in controller like:
public function __invoke(Project $project) {}