Laravel验证复选框 [英] Laravel validation checkbox

查看:293
本文介绍了Laravel验证复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel注册功能来注册用户.我在用户需要接受条款和条件的地方添加了一个复选框.我只希望用户在选中复选框时进行注册.我可以在laravel中使用必需"验证吗?这是我的验证功能:

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:

 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',
    ]);

当我使用这样的函数时,即使选中了laravel,laravel也会为该复选框提供所需的错误.

When I use the function like this, laravel gives the required error for the checkbox even if it is checked.

这是复选框的html

This is the html of the checkbox

<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>

希望你们能帮助我!

推荐答案

使用 规则.

正在验证的字段必须为yes,on,1或true.这对于验证服务条款"的接受很有用.

The field under validation must be yes, on, 1, or true. This is useful for validating "Terms of Service" acceptance.

您的案例样本:

 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'
]);

这篇关于Laravel验证复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆