php Laravel 验证复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37345363/
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 validation checkbox
提问by Sander Van Keer
I am using the laravel register function to register a user. I added a checkbox where the user needs to accept the terms and conditions. I only want to user to register when the checkbox is checked. Can I use the 'required' validation in laravel? This is my validation function:
我正在使用 laravel 注册功能来注册用户。我添加了一个复选框,用户需要在其中接受条款和条件。我只想让用户在选中复选框时进行注册。我可以在 Laravel 中使用“必需”验证吗?这是我的验证功能:
return Validator::make($data, [
'firstName' => 'required|max:255',
'lastName' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'checkbox' =>'required',
]);
When I use the function like this, laravel gives the required error for the checkbox even if it is checked.
当我使用这样的函数时,laravel 会为复选框提供所需的错误,即使它被选中。
This is the html of the checkbox
这是复选框的html
<input type="checkbox" name="checkbox" id="option" value="{{old('option')}}"><label for="option"><span></span> <p>Ik ga akkoord met de <a href="#">algemene voorwaarden</a></p></label>
I hope you guys can help me!
我希望你们能帮助我!
回答by SnakeDrak
Use the accepted
rule.
使用accepted
规则。
The field under validation must be yes, on, 1, or true. This is useful for validating "Terms of Service" acceptance.
验证中的字段必须是 yes、on、1 或 true。这对于验证“服务条款”的接受度很有用。
Sample for your case:
您的案例示例:
return Validator::make($data, [
'firstName' => 'required|max:255',
'lastName' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'checkbox' =>'accepted'
]);
回答by Calin Blaga
It will work, just be sure the input value will not be an empty string or false. And 'checkbox' =>'required' is ok as long as the key is the value of the input name attribute.
它会起作用,只要确保输入值不是空字符串或 false。'checkbox' =>'required' 只要键是输入名称属性的值就可以了。
回答by Tschallacka
I just had a big frustration, because the code i'm using returns the checkbox value as a boolean value.
我只是感到非常沮丧,因为我使用的代码将复选框值作为布尔值返回。
If you have a similar situation you can use the following rule:
如果您有类似情况,可以使用以下规则:
[
'checkbox_field' => 'required|in:1',
]
回答by Yasin Patel
Use required_without_allfor checkbox :
对复选框使用 required_without_all:
return Validator::make($data, [
'firstName' => 'required|max:255',
'lastName' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'checkbox' =>'required_without_all',
]);
Refer : https://laravel.com/docs/5.1/validation#available-validation-rules
参考:https: //laravel.com/docs/5.1/validation#available-validation-rules
回答by Steve
Your validation rules must corrolate with the name
attributes of your html form fields:
您的验证规则必须与name
html 表单字段的属性相关:
return Validator::make($data, [
'firstName' => 'required|max:255',
'lastName' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'option' =>'required', //not checkbox
]);