如果未选择任何选项,Laravel 将忽略选择输入验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46405564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel ignores select input validation if no option is selected
提问by Eseth
I have a form with some fields which I want to validate using Laravel's validate()
method.
我有一个包含一些字段的表单,我想使用 Laravel 的validate()
方法对其进行验证。
public function postSomething(Request $req) {
...
$this->validate($req, [
'text_input' => 'required',
'select_input' => 'required'
]);
...
}
The issue is that if the form is submitted without selecting an option from the select input it is ignored in the request and Laravel doesn't validate it despite the fact that it is added to the ruleset with the required
validation rule. Empty text inputs are being validated correctly.
问题是,如果提交表单时没有从选择输入中选择一个选项,它会在请求中被忽略,并且 Laravel 不会对其进行验证,尽管它已通过required
验证规则添加到规则集中。正在正确验证空文本输入。
+request: ParameterBag {#42 ▼
#parameters: array:1 [▼
"text_input" => ""
"_token" => "TCDqEi2dHVQfmc9HdNf8ju1ofdUQS6MtDBpUMkl7"
]
}
As you can see, the select_input
is missing from request parameters if it was left empty.
如您所见,select_input
如果将其留空,则请求参数中将缺少 。
Here is the HTML code for my select input:
这是我的选择输入的 HTML 代码:
<select class="form-control" name="select_input">
<option disabled selected>Please select...</option>
<option value="val1">Value 1</option>
<option value="val2">Value 2</option>
</select>
Is there a way to make the validation work for all fields from the ruleset even if some of them are not present in the request?
有没有办法对规则集中的所有字段进行验证,即使其中一些字段不存在于请求中?
From Laravel 5.1 validation documentation:
来自 Laravel 5.1 验证文档:
required
The field under validation must be present in the input dataand not empty. A field is considered "empty" is one of the following conditions are true: The value is null. The value is an empty string. The value is an empty array or empty Countable object. The value is an uploaded file with no path.
必需的
验证中的字段必须存在于输入数据中并且不能为空。一个字段被认为是“空”是在下列条件之一为真: 值为空。该值是一个空字符串。该值是一个空数组或空的 Countable 对象。该值是一个没有路径的上传文件。
P.S. I'm using Laravel 5.1, so present
method is not available.
PS 我使用的是 Laravel 5.1,所以present
方法不可用。
采纳答案by Saroj
Your html should look like this
你的 html 应该是这样的
<select class="form-control" name="select_input">
<option value="" selected >Please select...</option>
<option value="val1">Value 1</option>
<option value="val2">Value 2</option>
</select>
$this->validate($req, [
'text_input' => 'required',
'select_input' => 'required',
]);
If your select box values are integer then
you can use required
with integer
like
如果您选择框中的值是整数,那么你可以使用required
与integer
像
$this->validate($req, [
'text_input' => 'required',
'select_input' => 'required|integer',
]);
Or if you have limited options for that select box then you can use
或者,如果该选择框的选项有限,则可以使用
'select_input' => "required|in:val1,val2,val3",
回答by Oluwatobi Samuel Omisakin
There are few options I can recommend:
我可以推荐几个选项:
Manually validate the request without using the validation extended in the Controller, I.e:
//validator FACADE $ validator = Validator::make ($request->all(), [ // rules here ]);
By this you can monitor which fields are passed and which one are not passed.
Secondly, set a default value for the select list and check that value when you are validating in the Controller, that is, if you have this default value then nothing is selected. You definitely will have only the fields submitted in your Controller.
手动验证请求而不使用控制器中扩展的验证,即:
//validator FACADE $ validator = Validator::make ($request->all(), [ // rules here ]);
通过这种方式,您可以监控哪些字段通过了哪些字段没有通过。
其次,为选择列表设置一个默认值,并在您在 Controller 中验证时检查该值,也就是说,如果您有这个默认值,则不会选择任何内容。您肯定只会在您的控制器中提交字段。
回答by Morteza Rajabi
You made it's option disabled, so it won't send anything through your form.
您已禁用它的选项,因此它不会通过您的表单发送任何内容。
Change your select box to
将您的选择框更改为
<select class="form-control" name="select_input">
<option value="">Please select...</option>
<option value="val1">Value 1</option>
<option value="val2">Value 2</option>
</select>