Skip to content

Commit

Permalink
feat: add getRequiredValidatedBodyParam method
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasvantuycom authored Aug 25, 2024
1 parent 26a1a0c commit d4c40ee
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/web/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit d4c40ee

Please sign in to comment.