Skip to content

Commit fd038e4

Browse files
authored
docs(core): document the DateFilter input format (#2360)
1 parent 8e0a8ea commit fd038e4

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

‎core/doctrine-filters.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,40 @@ class Offer
844844
> For other syntaxes, for e.g., if you want to new syntax with the ApiResource attribute take a look
845845
> [in the Introduction section](#introduction).
846846

847+
### Input format
848+
849+
Since API Platform 5.1, the `format` argument of the ORM and ODM `DateFilter` restricts the accepted
850+
input to a single
851+
[date format](https://www.php.net/manual/en/datetime.format.php#refsect1-datetime.format-parameters):
852+
853+
```php
854+
<?php
855+
// api/src/ApiResource/Article.php
856+
namespace App\ApiResource;
857+
858+
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
859+
use ApiPlatform\Metadata\GetCollection;
860+
use ApiPlatform\Metadata\QueryParameter;
861+
862+
#[GetCollection(
863+
parameters: [
864+
'publishedAt' => new QueryParameter(filter: new DateFilter(format: 'd/m/Y')),
865+
],
866+
)]
867+
class Article
868+
{
869+
// ...
870+
}
871+
```
872+
873+
A request such as `/articles?publishedAt[before]=20/03/2022` is then parsed with
874+
`DateTimeImmutable::createFromFormat`. Parsing is strict: the time part resets to midnight when the
875+
format does not define it, and values that overflow (such as `32/03/2022`) or carry trailing data
876+
are rejected. Like any other invalid date, a rejected value is ignored by the filter. The documented
877+
JSON Schema and OpenAPI type of the parameter is a plain `string`, without `format: date`.
878+
879+
Without `format`, the filter accepts any value supported by the `\DateTime` constructor.
880+
847881
### Date Filter using the ApiFilter Attribute Syntax (not recommended)
848882

849883
Basically the ApiFilter declares the correct service under the hood. We recommend to use

0 commit comments

Comments
 (0)