This is what I do: BEFORE the checkbox, place a hidden textbox like so (the order is important.. hidden textbox with value 0 has to be placed before the checkbox):
{{Form::hidden('myCheck',0)}}
{{Form::checkbox('myCheck')}}
You can use a macro to simplify this.
Now, when checkbox is not checked, 0 is sent in POST array. When it is checked, the value of checkbox supersedes the hidden textbox.
I've quickly came up with simple solution, allowing to validate a list of checkbox values, since I had many on my form. In one of the blogs, some developer mentioned that the hidden field method won't save the old values if your form didn't pass the validation.
$chks = array('multicolor','resizable','showRuler','namesNumbersEnabled');
foreach ($chks as $chk) {
$product->setAttribute($chk, (Input::has($chk)) ? true : false);
}
Here's solution from http://nielson.io/2014/02/handling-checkbox-input-in-laravel/
{!! Form::hidden('completed', false) !!}
{!! Form::checkbox('completed', true) !!}
liveart said:
I've quickly came up with simple solution, allowing to validate a list of checkbox values, since I had many on my form. In one of the blogs, some developer mentioned that the hidden field method won't save the old values if your form didn't pass the validation.
$chks = array('multicolor','resizable','showRuler','namesNumbersEnabled'); foreach ($chks as $chk) { $product->setAttribute($chk, (Input::has($chk)) ? true : false); }
I think this is the best practice at the moment to handle this situation. The only downside to this is that in a situation where you have checkboxes as much as 20 - 30 or more, you have to define all your checkbox names in the array, which nullifies the effect of using $request->all() or Input::all()
I did this:
if(! isset( $request['checkbox_name'] ) { $request('checkbox_name) = '0' }
Disadvantage is that this field is not required to be checked, so if it's not checked validation pass, and only after validation I am saying that it's equals to 0. So I need to make sure it's 0 after I am declaring that, maybe :)
edwingure said:
I did this:
if(! isset( $request['checkbox_name'] ) { $request('checkbox_name) = '0' }
Disadvantage is that this field is not required to be checked, so if it's not checked validation pass, and only after validation I am saying that it's equals to 0. So I need to make sure it's 0 after I am declaring that, maybe :)
$model->checkbox_name = isset($request['checkbox_name']); // same, cleaner
View:
<input type="hidden" name="testcheck" value="0">
<input type="checkbox" name="test" @if(old('test') !== NULL || old('test check') === NULL){{ 'checked' }}@endif>
Controller
I also added the following line for validation:
'testcheck' => 'required_without:test'
The post is 3 years ago, but if someone find to best solution I propuse this:
Laravel 5.5
$request->has('HTMLname_Checkbox')
The response is false (if checkbox is unchecked) or true (if checkbox is checked). In the HTML file you just put a simple checkbox element
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community