Skip to content

Commit c825db4

Browse files
authored
minor #925 [Docs] Resource validation (loic425)
This PR was merged into the 1.11 branch. Discussion ---------- | Q | A | --------------- | ----- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT Commits ------- 19f03f4 [Docs] Resource validation
2 parents 2ce6386 + 19f03f4 commit c825db4

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

docs/configure_your_operations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,4 +511,4 @@ These vars will be available on any operations for this resource.
511511

512512
**[Go back to the documentation's index](index.md)**
513513

514-
**[> Next chapter: Redirect](redirect.md)**
514+
**[> Next chapter: Validation](validation.md)**

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ So far we support:
2222
* [Create a new resource](create_new_resource.md)
2323
* [Configure your resource](configure_your_resource.md)
2424
* [Configure your operations](configure_your_operations.md)
25+
* [Validation](validation.md)
2526
* [Redirect](redirect.md)
2627
* [Resource Factories](resource_factories.md)
2728
* [Providers](providers.md)

docs/validation.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)