One of the annoyances of HTML form processing is that checkboxes are not returned if they are unchecked.
This causes an issue if you just want to use Laravel’s automatic validation of forms and then want to be able to pass the validated form response to the model. Whilst it is possible to manage checkboxes in the controller it always strikes me as messy. My solution is below. There will always be detractors that claim the validator is not the place for this, my argument is that I am validating that what comes from the validator is either true or false and not true or missing.
Since the rules area of the request object is actually a method, it is possible to interact with the content of the request.
So, in my EditUserRequest class, where I have a checkbox named ‘is_admin’;
public function rules() { // default the value of the is_admin checkbox $this->merge(['is_admin' => $this->input('is_admin', 0)]); return [ 'name' => 'required|min:5', 'email' => 'required|email', ]; }
I merge back into the request, the value of the input, or a default (the second option to Request->input) of 0. This sets the checkbox element to 0 if it is not present.
Then in the controller, I can use the simple;
$user->update($request->all());
I tried your method, but it doesn’t work if you include the “boolean” validation rule and the checkbox is true. I don’t have a clue as to why it wasn’t working. However the method they use on this linked page works with the validation rule. It’s very similar to yours, only located in the validator function instead of rules().
Thanks for the feedback, but I am unable to replicate your findings. If I add boolean as a rule for the checkbox it works fine, although it should be noted that the rule is bypassed if the checkbox is unchecked (its not possible to have required|boolean) since false is the same as not present as far as the validator is concerned.
You say it was not working – not working in what regard?
I just want to confirm IT IS WORKING. I just tried it with a fresh Laravel 5.1 install.
Thank you very much! It’s by far the cleaner solution I have found.
I also found this technique (http://nielson.io/2014/02/handling-checkbox-input-in-laravel/) but I prefer yours and agree it is a good place to handle that in a Form Request Class.
Thank you!
Thanks for the feedback Jong and the link to the other article which is an alternate approach, but not without its own problems it seems.
great stuff – works like a charm! thanks Mark!
Very handy idea! Thanks for posting
Hey, Thanks a lot for posting.
I was about to leave Laravel for this very basic issue 🙂 Thank you … It works nice. Anyone can’t make it run, should follow Laracasts Larave 5 Fundamentals series by Jeffrey Way.
You can just use a “sometimes|accepted” rule.
No you cannot. If you want to pass the request directly to the model then the checkbox will be absent in the request if it was not checked. This means you can set the checkbox but you cannot unset it. We are only touching validation as ONE way to solve the problem.