Skip to content

Commit

Permalink
minor #925 [Docs] Resource validation (loic425)
Browse files Browse the repository at this point in the history
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
GSadee authored Sep 5, 2024
2 parents 2ce6386 + 19f03f4 commit c825db4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/configure_your_operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,4 @@ These vars will be available on any operations for this resource.

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

**[> Next chapter: Redirect](redirect.md)**
**[> Next chapter: Validation](validation.md)**
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ So far we support:
* [Create a new resource](create_new_resource.md)
* [Configure your resource](configure_your_resource.md)
* [Configure your operations](configure_your_operations.md)
* [Validation](validation.md)
* [Redirect](redirect.md)
* [Resource Factories](resource_factories.md)
* [Providers](providers.md)
Expand Down
92 changes: 92 additions & 0 deletions docs/validation.md
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)**

0 comments on commit c825db4

Please sign in to comment.