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
feature #54153 [HttpKernel] allow boolean argument support for MapQueryString (Jean-Beru)
This PR was squashed before being merged into the 7.1 branch.
Discussion
----------
[HttpKernel] allow boolean argument support for MapQueryString
| Q | A
| ------------- | ---
| Branch? | 7.1
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Issues | Fix #53616
| License | MIT
~~Add a `$filters` argument to the `MapQueryString` attribute to allow filtering parameters using `filter_var` function. It will be useful to cast "false", "0", "no" or "off" to `false` before denormalizing the request data.~~
**EDIT**
Add a `AbstractNormalizer::FILTER_BOOL` context option to cast to a boolean using the `filter_var` function with the `\FILTER_VALIDATE_BOOL` filter (see https://www.php.net/manual/fr/filter.filters.validate.php).
`MapQueryString` will use this context option when denormalizing the query string.
`MapPayload` also but only with data coming from a form.
See the relative comment: symfony/symfony#54153 (comment)
Example:
```php
final readonly class SearchDto
{
public function __construct(public ?bool $restricted = null)
{
}
}
final class MyController
{
#[Route(name: 'search', path: '/search')]
public function __invoke(#[MapQueryString] SearchDto $search): Response
{
// /search?restricted=
// "true", "1", "yes" and "on" will be cast to true
// "false", "0", "no", "off" and "" will be cast to false
// other will be cast to null
}
}
```
Commits
-------
5c20295662 [HttpKernel] allow boolean argument support for MapQueryString
sprintf('Failed to create object because the class "%s" is not instantiable.', $class),
441
-
$data,
442
-
['unknown'],
443
-
$context['deserialization_path'] ?? null
444
-
);
449
+
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Failed to create object because the class "%s" is not instantiable.', $class), $data, ['unknown'], $context['deserialization_path'] ?? null);
445
450
}
446
451
447
452
returnnew$class();
@@ -473,7 +478,9 @@ protected function denormalizeParameter(\ReflectionClass $class, \ReflectionPara
0 commit comments