55

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

143

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'
]);
Sign up to request clarification or add additional context in comments.

2 Comments

This is the correct answer for Terms of Service checkboxes.
This bypassed the server-side terms accepting validation if post is made manually. The solution is to put accepted first then required.
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.

Comments

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

Comments

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

1 Comment

I know this is an old question, but why do you suggest "required_without_all:foo,bar,..."? I mean, the documentation says «The field under validation must be present only when all of the other specified fields are not present.» so it seems an unrelated use case, not for general checkboxes.
0
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 Comment

Can you share a bit "text" with your code?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.