Skip to content

Commit 96c9cc5

Browse files
authored
Merge pull request #516 from toofff/feature/update-validation-groups
Feat: Add validation groups on operations
2 parents d759422 + 4999031 commit 96c9cc5

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

core/operations.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use ApiPlatform\Core\Annotation\ApiResource;
6767
* @ApiResource(
6868
* collectionOperations={"get"},
6969
* itemOperations={"get"}
70-
* )
70+
* )
7171
*/
7272
class Book
7373
{
@@ -88,7 +88,7 @@ use ApiPlatform\Core\Annotation\ApiResource;
8888
* @ApiResource(
8989
* collectionOperations={"get"={"method"="GET"}},
9090
* itemOperations={"get"={"method"="GET"}}
91-
* )
91+
* )
9292
*/
9393
class Book
9494
{

core/validation.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,70 @@ Of course, you can use XML or YAML configuration format instead of annotations i
167167
You may also pass in a [group sequence](http://symfony.com/doc/current/validation/sequence_provider.html) in place of
168168
the array of group names.
169169

170+
## Using Validation Groups on Operations
171+
172+
You can have different validation for each [operation](operations.md) related to your resource.
173+
174+
```php
175+
<?php
176+
// api/src/Entity/Book.php
177+
178+
use ApiPlatform\Core\Annotation\ApiResource;
179+
use Symfony\Component\Validator\Constraints as Assert;
180+
181+
/**
182+
* @ApiResource(
183+
* collectionOperations={
184+
* "get",
185+
* "post"={"validation_groups"={"Default", "postValidation"}}
186+
* },
187+
* itemOperations={
188+
* "delete",
189+
* "get",
190+
* "put"={"validation_groups"={"Default", "putValidation"}}
191+
* }
192+
* )
193+
* ...
194+
*/
195+
class Book
196+
{
197+
/**
198+
* @Assert\Uuid
199+
*/
200+
private $id;
201+
202+
/**
203+
* @Assert\NotBlank(groups={"postValidation"})
204+
*/
205+
private $name;
206+
207+
/**
208+
* @Assert\NotNull
209+
* @Assert\Length(
210+
* min = 2,
211+
* max = 50,
212+
* groups={"postValidation"}
213+
* )
214+
* @Assert\Length(
215+
* min = 2,
216+
* max = 70,
217+
* groups={"putValidation"}
218+
* )
219+
*/
220+
private $author;
221+
222+
// ...
223+
}
224+
```
225+
226+
With this configuration, there are three validation groups:
227+
228+
`Default` contains the constraints that belong to no other group.
229+
230+
`postValidation` contains the constraints on the name and author (length from 2 to 50) fields only.
231+
232+
`putValidation` contains the constraints on the author (length from 2 to 70) field only.
233+
170234
## Dynamic Validation Groups
171235

172236
If you need to dynamically determine which validation groups to use for an entity in different scenarios, just pass in a

index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@
6060
11. [Embedding the JSON-LD Context](core/serialization.md#embedding-the-json-ld-context)
6161
8. [Validation](core/validation.md)
6262
1. [Using Validation Groups](core/validation.md#using-validation-groups)
63-
2. [Dynamic Validation Groups](core/validation.md#dynamic-validation-groups)
64-
3. [Error Levels and Payload Serialization](core/validation.md#error-levels-and-payload-serialization)
63+
2. [Using Validation Groups on operations](core/validation.md#using-validation-groups-on-operations)
64+
3. [Dynamic Validation Groups](core/validation.md#dynamic-validation-groups)
65+
4. [Error Levels and Payload Serialization](core/validation.md#error-levels-and-payload-serialization)
6566
9. [Error Handling](core/errors.md)
6667
1. [Converting PHP Exceptions to HTTP Errors](core/errors.md#converting-php-exceptions-to-http-errors)
6768
10. [Pagination](core/pagination.md)

0 commit comments

Comments
 (0)