-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
3 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Validation | ||
|
||
<!-- TOC --> | ||
* [on HTML request](#on-html-request) | ||
* [on API request](#on-api-request) | ||
* [Disable validation](#disable-validation) | ||
<!-- TOC --> | ||
|
||
It uses `symfony/validator` to validate your data. | ||
|
||
## on HTML request | ||
|
||
```php | ||
namespace App\Entity; | ||
|
||
use App\Form\Type\BookType; | ||
use Sylius\Resource\Model\ResourceInterface; | ||
use Sylius\Resource\Metadata\AsResource; | ||
use Sylius\Resource\Metadata\Create; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
#[AsResource( | ||
formType: BookType::class, | ||
) | ||
#[Create] | ||
class Book implements ResourceInterface | ||
{ | ||
// ... | ||
#[Assert\NotBlank()] | ||
private ?string $title; | ||
} | ||
``` | ||
|
||
In this example, validation will fail when adding a new book without specifying its title on the form. | ||
|
||
## on API request | ||
|
||
```php | ||
namespace App\Entity; | ||
|
||
use App\Form\Type\BookType; | ||
use Sylius\Resource\Metadata\Api\Post; | ||
use Sylius\Resource\Model\ResourceInterface; | ||
use Sylius\Resource\Metadata\AsResource; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
#[AsResource() | ||
#[Post] | ||
class Book implements ResourceInterface | ||
{ | ||
// ... | ||
#[Assert\NotBlank()] | ||
private ?string $title; | ||
} | ||
``` | ||
|
||
In this example, validation will fail when adding a new book without specifying its title on the payload. | ||
|
||
## Disable validation | ||
|
||
In some case, you may want to disable this validation. | ||
|
||
For example, in a "publish" operation, you may want to apply a state machine transition without validation existing data. | ||
|
||
```php | ||
namespace App\BoardGameBlog\Infrastructure\Sylius\Resource; | ||
|
||
use App\BoardGameBlog\Infrastructure\Sylius\State\Http\Processor\PublishBoardGameProcessor; | ||
use App\BoardGameBlog\Infrastructure\Sylius\State\Http\Provider\BoardGameItemProvider; | ||
use App\BoardGameBlog\Infrastructure\Symfony\Form\Type\BoardGameType; | ||
use Sylius\Resource\Metadata\ApplyStateMachineTransition; | ||
use Sylius\Resource\Model\ResourceInterface; | ||
use Sylius\Resource\Metadata\AsResource; | ||
use Sylius\Resource\Metadata\Create; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
#[AsResource( | ||
formType: BoardGameType::class, | ||
) | ||
#[Update( | ||
provider: BoardGameItemProvider::class, | ||
processor: PublishBoardGameProcessor::class, | ||
validate: false, // disable resource validation | ||
)] | ||
class BoardGameResource implements ResourceInterface | ||
{ | ||
} | ||
``` | ||
|
||
**[Go back to the documentation's index](index.md)** | ||
|
||
**[> Next chapter: Redirect](redirect.md)** |