Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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);
}
Last updated 2 years ago.
0

Here's solution from http://nielson.io/2014/02/handling-checkbox-input-in-laravel/

{!! Form::hidden('completed', false) !!}
{!! Form::checkbox('completed', true) !!}
Last updated 9 years ago.
0

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

0

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

Last updated 8 years ago.
0

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

0

Solution with Laravel 5.4

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

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

0

Sign in to participate in this thread!

Eventy

Your banner here too?

isomis isomis Joined 7 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.