53

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

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

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>

I hope you guys can help me!

3
  • What does your form look like? Did you check the actual post data in dev tools? Probably the html is wrong so the data isnt being posted. Top tip - if you are using a modern IDE, install xdebug and set a breakpoint
    – Steve
    Commented May 20, 2016 at 11:25
  • 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. Commented May 20, 2016 at 11:40
  • Thanks man! Can you post this as an answer? This way I can accept it and upote it Commented May 20, 2016 at 11:45

5 Answers 5

140

Use the accepted rule.

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

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'
]);
2
  • 24
    This is the correct answer for Terms of Service checkboxes. Commented Aug 13, 2018 at 19:59
  • This bypassed the server-side terms accepting validation if post is made manually. The solution is to put accepted first then required.
    – Henrikh
    Commented Jan 14, 2022 at 9:22
8

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.

6

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',
]
4

Use required_without_all for checkbox :

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

0
1
public function store(Request $request)
    {
        $datos=$request->validate([
            'name'        =>'required|max:60',
            'descrip'   =>'nullable|max:255',
            'finish'    =>'nullable', //this is the checkbox
            'urgent'      =>'required|numeric|min:0|max:2',
            'limit'  =>'required|date-format:Y-m-d\TH:i'
        ]);

        
        if(isset($datos['finish'])){
            if ($datos['finish']=="on"){
                $datos['finish']=1;
            }
        }else{
            $datos['finish']=0;
        }
        $tarea=Tarea::create($datos);
        return redirect()->route('tarea.index');

    }
1
  • 1
    Can you share a bit "text" with your code?
    – KargWare
    Commented Dec 12, 2022 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.