[英]Lumen Validation allow only one Get Parameter
In my project I use Lumen and I want to implement some kind of validation for my GET request.在我的项目中,我使用 Lumen 并且我想为我的 GET 请求实施某种验证。
The example URL would look like this:示例 URL 如下所示:
[URL]/blubb/bla?tags[]=1&tags[]=2
[URL]/blubb/bla?tags[]=1&tags[]=2
In my code I validate the URL like this:在我的代码中,我像这样验证 URL:
public function handleRequest(Request $request)
{
try {
$validatedData = $this->validate($request, [
'tags' => 'nullable|array'
]);
} catch (ValidationException $th) {
// return Error.
}
// go on.
}
My problem is that if a user uses an URL like this one, the validation does not trigger and the "go on."我的问题是,如果用户使用这样的 URL,验证不会触发并且“继续”。 part is called.
部分被称为。
[URL]/blubb/bla?invalidParameter=1
[URL]/blubb/bla?invalidParameter=1
Is there a way to only allow a single "kind" of GET Parameter?有没有办法只允许一个“种类”的 GET 参数?
EDIT: The "tags" is nullable because my API endpoint can be called without any GET parameters.编辑:“标签”可以为空,因为可以在没有任何 GET 参数的情况下调用我的 API 端点。
You could get the full array with $request->all()
and have a look at the keys.您可以使用
$request->all()
获取完整数组并查看键。
$paramValidation = $request->all()
unset $paramValidation['tags'];
if (count($paramValidation)) {
// error
}
However, maybe you just want to ignore other params.但是,也许您只想忽略其他参数。 Have a look at the method
$request->validated()
.看看方法
$request->validated()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.