spiral-packages / symfony-validator
Symfony validator bridge for Spiral Framework
Package info
github.com/spiral-packages/symfony-validator
pkg:composer/spiral-packages/symfony-validator
Fund package maintenance!
Requires
- php: ^8.2
- spiral/attributes: ^2.8 || ^3.0
- spiral/filters: ^3.1
- spiral/http: ^3.1
- spiral/validation: ^3.1
- symfony/cache: ^6.4 || ^7.0 || ^8.0
- symfony/psr-http-message-bridge: ^6.4 || ^7.0 || ^8.0
- symfony/validator: ^6.4 || ^7.0 || ^8.0
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.8
- buggregator/trap: ^1.13
- llm/skills: ^1.23
- nyholm/psr7-server: ^1.1
- roxblnfk/unpoly: ^1.8
- spiral/framework: ^3.12
- spiral/nyholm-bridge: ^1.2
- testo/bridge-infection: ^0.1.9
- testo/testo: ^0.10.55
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Symfony Validator bridge for Spiral Framework
This package lets Spiral Filters be validated with Symfony Validator constraints, declared as attributes on filter properties or as an array of rules in a filter definition.
Get Started
Installation
composer require spiral-packages/symfony-validator
Requirements: PHP 8.2+, Spiral Framework 3.x and Symfony Validator 6.4, 7 or 8.
Configuration
Register the bootloader in your application kernel:
public function defineBootloaders(): array { return [ // ... \Spiral\Validation\Symfony\Bootloader\ValidatorBootloader::class, // ... ]; }
With spiral-packages/discoverer the bootloader is registered
automatically. It makes the Symfony validator the default one for filters and adds the symfony-file input source
for uploaded files.
Usage
Extend AttributesFilter and put Symfony constraints next to the input attributes. Uploaded files are read with the
File attribute and arrive as Symfony UploadedFile objects, so file constraints such as Image apply to them:
use Spiral\Filters\Attribute\Input\Post; use Spiral\Validation\Symfony\Attribute\Input\File; use Spiral\Validation\Symfony\AttributesFilter; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\Validator\Constraints; final class CreatePostFilter extends AttributesFilter { #[Post] #[Constraints\NotBlank] #[Constraints\Length(min: 5)] public string $title; #[Post] #[Constraints\NotBlank] #[Constraints\Positive] public int $sort; #[File] #[Constraints\Image] public UploadedFile $image; }
The rules and the mapping can also be returned from a filter definition:
use Spiral\Filters\Model\Filter; use Spiral\Filters\Model\FilterDefinitionInterface; use Spiral\Filters\Model\HasFilterDefinition; use Spiral\Validation\Symfony\FilterDefinition; use Symfony\Component\Validator\Constraints\Email; use Symfony\Component\Validator\Constraints\Image; use Symfony\Component\Validator\Constraints\NotBlank; final class CreateUserFilter extends Filter implements HasFilterDefinition { public function filterDefinition(): FilterDefinitionInterface { return new FilterDefinition( validationRules: [ 'username' => [new NotBlank()], 'email' => [new NotBlank(), new Email()], 'image' => [new Image()], ], mappingSchema: [ 'username' => 'username', 'email' => 'email', 'image' => 'symfony-file:image', ], ); } }
Inject the filter into a controller action: the request data is mapped and validated before the action runs, and invalid input is rejected with the constraint violation messages.
See the documentation for more details.
