|
| 1 | +# Validation |
| 2 | + |
| 3 | +<!-- TOC --> |
| 4 | +* [on HTML request](#on-html-request) |
| 5 | +* [on API request](#on-api-request) |
| 6 | +* [Disable validation](#disable-validation) |
| 7 | +<!-- TOC --> |
| 8 | + |
| 9 | +It uses `symfony/validator` to validate your data. |
| 10 | + |
| 11 | +## on HTML request |
| 12 | + |
| 13 | +```php |
| 14 | +namespace App\Entity; |
| 15 | + |
| 16 | +use App\Form\Type\BookType; |
| 17 | +use Sylius\Resource\Model\ResourceInterface; |
| 18 | +use Sylius\Resource\Metadata\AsResource; |
| 19 | +use Sylius\Resource\Metadata\Create; |
| 20 | +use Symfony\Component\Validator\Constraints as Assert; |
| 21 | + |
| 22 | +#[AsResource( |
| 23 | + formType: BookType::class, |
| 24 | +) |
| 25 | +#[Create] |
| 26 | +class Book implements ResourceInterface |
| 27 | +{ |
| 28 | + // ... |
| 29 | + #[Assert\NotBlank()] |
| 30 | + private ?string $title; |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +In this example, validation will fail when adding a new book without specifying its title on the form. |
| 35 | + |
| 36 | +## on API request |
| 37 | + |
| 38 | +```php |
| 39 | +namespace App\Entity; |
| 40 | + |
| 41 | +use App\Form\Type\BookType; |
| 42 | +use Sylius\Resource\Metadata\Api\Post; |
| 43 | +use Sylius\Resource\Model\ResourceInterface; |
| 44 | +use Sylius\Resource\Metadata\AsResource; |
| 45 | +use Symfony\Component\Validator\Constraints as Assert; |
| 46 | + |
| 47 | +#[AsResource() |
| 48 | +#[Post] |
| 49 | +class Book implements ResourceInterface |
| 50 | +{ |
| 51 | + // ... |
| 52 | + #[Assert\NotBlank()] |
| 53 | + private ?string $title; |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +In this example, validation will fail when adding a new book without specifying its title on the payload. |
| 58 | + |
| 59 | +## Disable validation |
| 60 | + |
| 61 | +In some case, you may want to disable this validation. |
| 62 | + |
| 63 | +For example, in a "publish" operation, you may want to apply a state machine transition without validation existing data. |
| 64 | + |
| 65 | +```php |
| 66 | +namespace App\BoardGameBlog\Infrastructure\Sylius\Resource; |
| 67 | + |
| 68 | +use App\BoardGameBlog\Infrastructure\Sylius\State\Http\Processor\PublishBoardGameProcessor; |
| 69 | +use App\BoardGameBlog\Infrastructure\Sylius\State\Http\Provider\BoardGameItemProvider; |
| 70 | +use App\BoardGameBlog\Infrastructure\Symfony\Form\Type\BoardGameType; |
| 71 | +use Sylius\Resource\Metadata\ApplyStateMachineTransition; |
| 72 | +use Sylius\Resource\Model\ResourceInterface; |
| 73 | +use Sylius\Resource\Metadata\AsResource; |
| 74 | +use Sylius\Resource\Metadata\Create; |
| 75 | +use Symfony\Component\Validator\Constraints as Assert; |
| 76 | + |
| 77 | +#[AsResource( |
| 78 | + formType: BoardGameType::class, |
| 79 | +) |
| 80 | +#[Update( |
| 81 | + provider: BoardGameItemProvider::class, |
| 82 | + processor: PublishBoardGameProcessor::class, |
| 83 | + validate: false, // disable resource validation |
| 84 | + )] |
| 85 | +class BoardGameResource implements ResourceInterface |
| 86 | +{ |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +**[Go back to the documentation's index](index.md)** |
| 91 | + |
| 92 | +**[> Next chapter: Redirect](redirect.md)** |
0 commit comments