How to handle unchecked checkboxes in Laravel?

I have an edit form with several checkboxes in Laravel. When I check the boxes and update the record, it works correctly, but if I uncheck the box it doesn't return any value so the record doesn't update and the value remains true in the database.

How can I handle unchecked checkboxes in Laravel?

Upvotes: 16

Views: 31279

Answers (8)

Damiano
Damiano

Reputation: 1

$request->checkbox ?? $request->merge(['checkbox' => $value]);

If $request->checkbox is null it is replaced by the proper $value (i.e. 0) in the $request, the you can just "update" the usual way.

Upvotes: 0

p431i7o
p431i7o

Reputation: 11

I wanted to contribute with my example (In this context laravel 9.x and bootstrap 5 are used, but the logic behind is the same as other aswers, only that this will not send two fields with the same name, because this can produce ambiguous results depending of wich fields will be read last or first by PHP interpreter)

in my validation rule I have something like this (my field name is 'active'):

'active'=>'required|boolean|in:1,0'

So in the blade rendering part I wrote this:

<div class="form-check form-switch">
  <input id="active" type="hidden" name="active" value="@if(!empty($record->active)) 1 @else 0 @endif">
  <input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault" @checked($record->active) onclick="document.getElementById('active').value=this.checked?1:0">
  <label class="form-check-label" for="flexSwitchCheckDefault">Is Active</label>
</div>

In this way, always a 0 or a 1 will be sent

Upvotes: 0

Dev2Chartreuse
Dev2Chartreuse

Reputation: 1

maybe you can just check if the input checkbox exists, if it is exists, $value equals input checkbox value, and if it doesnt exists that means checkbox is unchecked, so $value is false. You also have to add a form request to be sure that the input contains a boolean

$value = $request->has('checkbox') ? $request->input('checkbox') : false

Upvotes: 0

Wajih
Wajih

Reputation: 4383

Try this

<input type="hidden" name="param" value="0">
<input type="checkbox" name="param" value="1">

if checkbox is checked => server get "1"

otherwise => server get "0"

Upvotes: 29

Mr.Web
Mr.Web

Reputation: 7154

The best way I can think of is using ternary and null coalesce PHP operators:

$request->checkbox ? 1 : 0 ?? 0;

This will return 1 if checked, 0 if not checked or not available.

Upvotes: 6

Jomoos
Jomoos

Reputation: 13083

From mozilla documentation on checkbox:

If a checkbox is unchecked when its form is submitted, there is no value submitted to the server to represent its unchecked state (e.g. value=unchecked); the value is not submitted to the server at all.

So, the following will do the trick:

$isChecked = $request->has('checkbox-name');

Upvotes: 21

shushu304
shushu304

Reputation: 1498

In HTML forms when you check checkbox it's arrived with the POST,

and you don't check it ... so it's not exists at all in the POST.

so you need to check if it's exists ...

if($request->has('input-name')) {
     $value = $request->input('input-name')
}
else {
     $value = "put default value (false)";
}
.... 
do your stuff with the value

Upvotes: 1

Place a hidden textbox with a 0 value before the checkbox so a 0 value is sent into the POST array.

{{Form::hidden('value',0)}}
{{Form::checkbox('value')}}

Upvotes: 32

Related Questions