You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add notes on automatic validation benefits and reference `ParameterValidatorProvider` for Symfony and Laravel. Clarify the usage of `ParameterExtension` for handling empty values in Doctrine ORM.
Copy file name to clipboardExpand all lines: core/doctrine-filters.md
+24-5Lines changed: 24 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -181,7 +181,7 @@ services all begin with `api_platform.doctrine_mongodb.odm`.
181
181
### Built-in new Search Filters (API Platform >= 4.2)
182
182
183
183
To add some search filters, choose over this new list:
184
-
- [IriFilter](#iri-filter) (filter on IRIs)
184
+
- [IriFilter](#iri-filter) (filter on IRIs)
185
185
- [ExactFilter](#exact-filter) (filter with exact value)
186
186
- [PartialSearchFilter](#partial-search-filter) (filter using a `LIKE %value%``)
187
187
- [FreeTextQueryFilter](#free-text-query-filter) (allows you to apply multiple filters to multiple properties of a resource at the same time, using a single parameter in the URL)
@@ -1534,6 +1534,18 @@ To validate inputs and ensure the correct type, we can implement the `JsonSchema
1534
1534
1535
1535
This allows delegating validation to API Platform, respecting the [SOLID Principles](https://en.wikipedia.org/wiki/SOLID).
1536
1536
1537
+
> [!NOTE]
1538
+
> Even with our internal systems, some additional **manual validation** is needed to ensure greater accuracy. However,
1539
+
> we already take care of a lot of these validations for you.
1540
+
>
1541
+
> You can see how this works directly in our code components:
1542
+
>
1543
+
> * The `ParameterValidatorProvider` for **Symfony** can be found [here](https://github.com/api-platform/core/blob/c9692b509d5b641104addbadb349b9bcab83e251/src/Symfony/Validator/State/ParameterValidatorProvider.php).
1544
+
> * The `ParameterValidatorProvider` for **Laravel** is located [here](https://github.com/api-platform/core/blob/c9692b509d5b641104addbadb349b9bcab83e251/src/Laravel/State/ParameterValidatorProvider.php).
1545
+
>
1546
+
> Additionally, we filter out empty values within our `ParameterExtension` classes. For instance, the **Doctrine ORM**
1547
+
> `ParameterExtension` [handles this filtering here](https://github.com/api-platform/core/blob/c9692b509d5b641104addbadb349b9bcab83e251/src/Doctrine/Orm/Extension/ParameterExtension.php#L51C13-L53C14).
1548
+
1537
1549
```php
1538
1550
<?php
1539
1551
@@ -1560,10 +1572,17 @@ final class MonthFilter implements FilterInterface, JsonSchemaFilterInterface
1560
1572
}
1561
1573
```
1562
1574
1563
-
With this code, under the hood, API Platform has added a [Symfony Range constraint](https://symfony.com/doc/current/reference/constraints/Range.html)
1564
-
that only accepts values between `1` and `12` (inclusive), which is what we want. In addition, we map the value to an integer,
1565
-
which allows us to reject other types and directly return an integer in our filter when we retrieve the value with
1566
-
`$monthValue = $parameter->getValue();`.
1575
+
With this code, under the hood, API Platform automatically adds a [Symfony Range constraint](https://symfony.com/doc/current/reference/constraints/Range.html).
1576
+
This ensures the parameter only accepts values between `1` and `12` (inclusive), which is exactly what we need.
1577
+
1578
+
This approach offers two key benefits:
1579
+
1580
+
- Automatic Validation: It rejects other data types and invalid values, so you get an integer directly.
1581
+
- Simplified Logic: You can retrieve the value with `$monthValue = $parameter->getValue();` knowing it's already a
1582
+
- validated integer.
1583
+
1584
+
This means you **don't have to add custom validation to your filter class, entity, or model**. The validation is handled
1585
+
for you, making your code cleaner and more efficient.
0 commit comments