mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-02 13:02:24 +08:00
Fix #18450: Allow empty string to be passed as a nullable typed argument to a controller's action
This commit is contained in:
@ -23,6 +23,7 @@ Yii Framework 2 Change Log
|
||||
- Bug #18508: Fix Postgres SQL query for load table indexes with correct column order (insolita)
|
||||
- Enh #18518: Add support for ngrok’s `X-Original-Host` header (brandonkelly)
|
||||
- Bug #18529: Fix asset files path with `appendTimestamp` option for non-root-relative base URLs (bizley)
|
||||
- Bug #18450: Allow empty string to be passed as a nullable typed argument to a controller's action (dicrtarasov, bizley)
|
||||
|
||||
|
||||
2.0.40 December 23, 2020
|
||||
|
||||
@ -142,31 +142,38 @@ class Controller extends \yii\base\Controller
|
||||
} elseif (is_array($params[$name])) {
|
||||
$isValid = false;
|
||||
} elseif (
|
||||
PHP_VERSION_ID >= 70000 &&
|
||||
($type = $param->getType()) !== null &&
|
||||
$type->isBuiltin() &&
|
||||
($params[$name] !== null || !$type->allowsNull())
|
||||
PHP_VERSION_ID >= 70000
|
||||
&& ($type = $param->getType()) !== null
|
||||
&& $type->isBuiltin()
|
||||
&& ($params[$name] !== null || !$type->allowsNull())
|
||||
) {
|
||||
$typeName = PHP_VERSION_ID >= 70100 ? $type->getName() : (string)$type;
|
||||
switch ($typeName) {
|
||||
case 'int':
|
||||
$params[$name] = filter_var($params[$name], FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
|
||||
break;
|
||||
case 'float':
|
||||
$params[$name] = filter_var($params[$name], FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE);
|
||||
break;
|
||||
case 'bool':
|
||||
$params[$name] = filter_var($params[$name], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
||||
break;
|
||||
}
|
||||
if ($params[$name] === null) {
|
||||
$isValid = false;
|
||||
|
||||
if ($params[$name] === '' && $type->allowsNull()) {
|
||||
if ($typeName !== 'string') { // for old string behavior compatibility
|
||||
$params[$name] = null;
|
||||
}
|
||||
} else {
|
||||
switch ($typeName) {
|
||||
case 'int':
|
||||
$params[$name] = filter_var($params[$name], FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
|
||||
break;
|
||||
case 'float':
|
||||
$params[$name] = filter_var($params[$name], FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE);
|
||||
break;
|
||||
case 'bool':
|
||||
$params[$name] = filter_var($params[$name], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
||||
break;
|
||||
}
|
||||
if ($params[$name] === null) {
|
||||
$isValid = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$isValid) {
|
||||
throw new BadRequestHttpException(Yii::t('yii', 'Invalid data received for parameter "{param}".', [
|
||||
'param' => $name,
|
||||
]));
|
||||
throw new BadRequestHttpException(
|
||||
Yii::t('yii', 'Invalid data received for parameter "{param}".', ['param' => $name])
|
||||
);
|
||||
}
|
||||
$args[] = $actionParams[$name] = $params[$name];
|
||||
unset($params[$name]);
|
||||
@ -184,16 +191,16 @@ class Controller extends \yii\base\Controller
|
||||
}
|
||||
|
||||
if (!empty($missing)) {
|
||||
throw new BadRequestHttpException(Yii::t('yii', 'Missing required parameters: {params}', [
|
||||
'params' => implode(', ', $missing),
|
||||
]));
|
||||
throw new BadRequestHttpException(
|
||||
Yii::t('yii', 'Missing required parameters: {params}', ['params' => implode(', ', $missing)])
|
||||
);
|
||||
}
|
||||
|
||||
$this->actionParams = $actionParams;
|
||||
|
||||
// We use a different array here, specifically one that doesn't contain service instances but descriptions instead.
|
||||
if (\Yii::$app->requestedParams === null) {
|
||||
\Yii::$app->requestedParams = array_merge($actionParams, $requestedParams);
|
||||
if (Yii::$app->requestedParams === null) {
|
||||
Yii::$app->requestedParams = array_merge($actionParams, $requestedParams);
|
||||
}
|
||||
|
||||
return $args;
|
||||
|
||||
Reference in New Issue
Block a user