Skip to content

Feat: Add validation groups on operations #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use ApiPlatform\Core\Annotation\ApiResource;
* @ApiResource(
* collectionOperations={"get"},
* itemOperations={"get"}
* )
* )
*/
class Book
{
Expand All @@ -88,7 +88,7 @@ use ApiPlatform\Core\Annotation\ApiResource;
* @ApiResource(
* collectionOperations={"get"={"method"="GET"}},
* itemOperations={"get"={"method"="GET"}}
* )
* )
*/
class Book
{
Expand Down
64 changes: 64 additions & 0 deletions core/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,70 @@ Of course, you can use XML or YAML configuration format instead of annotations i
You may also pass in a [group sequence](http://symfony.com/doc/current/validation/sequence_provider.html) in place of
the array of group names.

## Using Validation Groups on Operations

You can have different validation for each [operation](operations.md) related to your resource.

```php
<?php
// api/src/Entity/Book.php

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ApiResource(
* collectionOperations={
* "get",
* "post"={"validation_groups"={"Default", "postValidation"}}
* },
* itemOperations={
* "delete",
* "get",
* "put"={"validation_groups"={"Default", "putValidation"}}
* }
* )
* ...
*/
class Book
{
/**
* @Assert\Uuid
*/
private $id;

/**
* @Assert\NotBlank(groups={"postValidation"})
*/
private $name;

/**
* @Assert\NotNull
* @Assert\Length(
* min = 2,
* max = 50,
* groups={"postValidation"}
* )
* @Assert\Length(
* min = 2,
* max = 70,
* groups={"putValidation"}
* )
*/
private $author;

// ...
}
```

With this configuration, there are three validation groups:

`Default` contains the constraints that belong to no other group.

`postValidation` contains the constraints on the name and author (length from 2 to 50) fields only.

`putValidation` contains the constraints on the author (length from 2 to 70) field only.

## Dynamic Validation Groups

If you need to dynamically determine which validation groups to use for an entity in different scenarios, just pass in a
Expand Down
5 changes: 3 additions & 2 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@
11. [Embedding the JSON-LD Context](core/serialization.md#embedding-the-json-ld-context)
8. [Validation](core/validation.md)
1. [Using Validation Groups](core/validation.md#using-validation-groups)
2. [Dynamic Validation Groups](core/validation.md#dynamic-validation-groups)
3. [Error Levels and Payload Serialization](core/validation.md#error-levels-and-payload-serialization)
2. [Using Validation Groups on operations](core/validation.md#using-validation-groups-on-operations)
3. [Dynamic Validation Groups](core/validation.md#dynamic-validation-groups)
4. [Error Levels and Payload Serialization](core/validation.md#error-levels-and-payload-serialization)
9. [Error Handling](core/errors.md)
1. [Converting PHP Exceptions to HTTP Errors](core/errors.md#converting-php-exceptions-to-http-errors)
10. [Pagination](core/pagination.md)
Expand Down