Skip to content

Commit a778916

Browse files
pierre-Hjonag
authored andcommitted
Add payload constraint option (api-platform#263)
* Add payload constraint option For api-platform/core#1364 * Add ability to define payload fields * Minor Markdown and wording fixes * Update TOC
1 parent 96fd3ea commit a778916

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

core/validation.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,33 @@ class Book
159159
}
160160
```
161161

162+
## Error Levels and Payload Serialization
163+
164+
As stated in the [Symfony documentation](https://symfony.com/doc/current/validation/severity.html), you can use the payload field to define error levels.
165+
You can retrieve the payload field by setting the `serialize_payload_fields` option to `true` in the API Platform config:
166+
167+
```yaml
168+
# app/config/config.yml
169+
170+
api_platform:
171+
validator:
172+
serialize_payload_fields: true
173+
```
174+
175+
Then, the serializer will return all payload values in the error response.
176+
177+
If you want to serialize only some payload fields, define them in the config like this:
178+
179+
```yaml
180+
# app/config/config.yml
181+
182+
api_platform:
183+
validator:
184+
serialize_payload_fields: [ severity, anotherPayloadField ]
185+
```
186+
187+
In this example, only `severity` and `anotherPayloadField` will be serialized.
188+
162189
Previous chapter: [Serialization Groups and Relations](serialization-groups-and-relations.md)
163190

164191
Next chapter: [Pagination](pagination.md)

index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
8. [Validation](core/validation.md)
5151
1. [Using Validation Groups](core/validation.md#using-validation-groups)
5252
2. [Dynamic Validation Groups](core/validation.md#dynamic-validation-groups)
53+
3. [Error Levels and Payload Serialization](core/validation.md#error-levels-and-payload-serialization)
5354
9. [Pagination](core/pagination.md)
5455
1. [Disabling the Pagination](core/pagination.md#disabling-the-pagination)
5556
2. [Changing the Number of Items per Page](core/pagination.md#changing-the-number-of-items-per-page)

0 commit comments

Comments
 (0)