From d4c40ee369e2fb6a571dfed55c666500b9e10073 Mon Sep 17 00:00:00 2001 From: Thomas Vantuycom <107400578+thomasvantuycom@users.noreply.github.com> Date: Sun, 25 Aug 2024 12:01:34 +0200 Subject: [PATCH 1/2] feat: add getRequiredValidatedBodyParam method --- src/web/Request.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/web/Request.php b/src/web/Request.php index 6e0eff2c47f..2fa5e2b4b17 100644 --- a/src/web/Request.php +++ b/src/web/Request.php @@ -925,6 +925,48 @@ public function getRequiredBodyParam(string $name): mixed throw new BadRequestHttpException("Request missing required body param"); } + /** + * Validates and returns the named request body parameter value, or bails on the request with a 400 error if that parameter doesn’t exist or doesn’t pass validation. + * + * --- + * + * ```php + * // get required and validated $_POST['foo'] + * $foo = Craft::$app->request->getRequiredValidatedBodyParam('foo'); + * + * // get required and validated $_POST['foo']['bar'] + * $bar = Craft::$app->request->getRequiredValidatedBodyParam('foo.bar'); + * ``` + * ```twig + * {# get required and validated $_POST['foo'] #} + * {% set foo = craft.app.request.getRequiredValidatedBodyParam('foo') %} + * + * {# get required and validated $_POST['foo']['bar'] #} + * {% set bar = craft.app.request.getRequiredValidatedBodyParam('foo.bar') %} + * ``` + * + * @param string $name The parameter name. + * @return mixed The parameter value + * @throws BadRequestHttpException if the request does not have the body param or if the param value doesn’t pass validation + * @see getBodyParam() + */ + public function getRequiredValidatedBodyParam(string $name): mixed + { + $value = $this->getBodyParam($name); + + if ($value === null) { + throw new BadRequestHttpException("Request missing required body param"); + } + + $value = Craft::$app->getSecurity()->validateData($value); + + if ($value === false) { + throw new BadRequestHttpException('Request contained an invalid body param'); + } + + return $value; + } + /** * Validates and returns the named request body parameter value, or bails on the request with a 400 error if that parameter doesn’t pass validation. * From 4460bbc3f901f692df6bcb6d311a2bf4ccde29c8 Mon Sep 17 00:00:00 2001 From: Thomas Vantuycom <107400578+thomasvantuycom@users.noreply.github.com> Date: Sun, 25 Aug 2024 12:10:44 +0200 Subject: [PATCH 2/2] fix style --- src/web/Request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/web/Request.php b/src/web/Request.php index 2fa5e2b4b17..97199d97d60 100644 --- a/src/web/Request.php +++ b/src/web/Request.php @@ -955,7 +955,7 @@ public function getRequiredValidatedBodyParam(string $name): mixed $value = $this->getBodyParam($name); if ($value === null) { - throw new BadRequestHttpException("Request missing required body param"); + throw new BadRequestHttpException("Request missing required body param"); } $value = Craft::$app->getSecurity()->validateData($value);