How should I work with checkbox in laravel? - laravel

In my create.blade.php I have the following code:
<div class="form-check">
<label for="active">active</label>
<input id="active" name = "active" type="checkbox" class="form-check-input" value="1">
</div>
And in my edit.blade.php the following:
<div class="form-check">
<label for="active">active</label>
<input id="active" name = "active" type="checkbox" class="form-check-input" value="{!!$company->
active!!}">
</div>
SQLFiddle of my db structure with data.
When submitting either with the checkbox checked they work fine
When editing a record with active set to 1, the checkbox isn't checked when opening the form
I know there's no value when not checked and when opening the checkbox isn't 'on' because the value is 1 instead of 'on'.
How can I resolve this problem?

It's actually an HTML problem, you are just using the input the wrong way, you have to use the checked instead of value:
<input
id="active"
name="active"
type="checkbox"
class="form-check-input"
checked="{{ $company->active ? 'checked' : '' }}"
>

Related

How to get read of automatic checked radio button in a Boolean data type in Thymeleaf, Springboot?

Im using spring-boot back-end with the fontend thyme-leaf . But the problem is when im putting Boolean data type in a radio button then the value which is set to 0 is automatically checked in the font end, event if i try to add th:checked="unchecked" its not working. My code is given bellow
<div class="form-group">
<label for="isDownloadable"> Is Downloadable</label> <br>
<input type="radio" th:field="*{isDownloadable}" value="1" th:checked="checked">Yes
<input type="radio" th:field="*{isDownloadable}" value="0"> No<br>
</div>
As here iv tried to cheack Yes radio button but its checked the No radio button in the front end.How to solve this problem ?
here is the output of my code given
If you want to use 0 and 1, you put need to condition and show data.Solution is
<div class="form-group">
<label for="isDownloadable"> Is Downloadable</label> <br>
<input type="radio" value="1" th:checked="${history.Shift == 1} ? ${true}">Yes
<input type="radio" value="0" th:checked="${history.Shift == 0} ? ${true}"> No<br>
</div>

Submit checkbox values to Controller

I am having the following form, which I would like to submit to my backend.
<form id="revisionFilter" action="{{ route('revision.filter') }}" method='POST'>
<input class="form-check-input" type="checkbox" value="" id="checkbox1">
<label class="form-check-label" for="checkbox1">
1
</label>
<input class="form-check-input" type="checkbox" value="" id="checkbox0">
<label class="form-check-label" for="checkbox0">
0
</label>
<label class="form-check-label" for="checkboxNull">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="checkboxNull">
Null
</label>
<input type="hidden" name='_method' value='POST'>
<input id="revisionFilterSubmit" type="submit" class='btn btn-danger btn-sm' value='Filter'>
</form>
My backend looks like the following:
routes:
Route::post('/revision/filter', 'RevisionController#filter')->name('revision.filter');
RevisionController:
public function filter(Request $request)
{
Log::info("Request: ");
Log::info($request);
return redirect()->route('revision.rindex');
}
My problem is that when I press the button I am getting redirected to:
The page has expired due to inactivity.
Please refresh and try again.
Instead I would like to see the request with the values of the checkboxes to implement the db saving functionality.
I appreciate your replies!
since I've already made a comment. I will put my answer in the answer box lol.
To answer you, you're not getting any checkbox value since you're checkboxes don't have any name attribute.
You need to put a name attribute to them. E.g.,
<input class="form-check-input" type="checkbox" value="" name="checkbox0" id="checkbox0">
From there, you should now be able to see the value of the checkbox via its name.
Just put same name to your checkbox.For example
<input class="form-check-input" type="checkbox" value="" id="checkbox1" name="revisionFilter">
<input class="form-check-input" type="checkbox" value="" id="checkbox2" name="revisionFilter">

How do I save the oldvalue if the input name is variable?

The oldvalue is usually set with the name attribute, in my case the name attribute is variable depending on the id name="option{{$question->id}}", so I can't find the way to save and load the olvalue, this is my code:
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
<input type="radio" name="option{{$question->id}}" id="question" autocomplete="off" value="1" data-oldvalue="{{old('option'.$question->id) }}"> Yes
</label>
<label class="btn btn-primary">
<input type="radio" name="option{{$question->id}}" id="question" autocomplete="off" value="0" data-oldvalue="{{old('option'.$question->id) }}"> No
</label>
</div>
Not sure if the If there is any other approach to get my oldvalues loaded (after validation errors) I would like to know.
Extra info: I'm not sure if the reason the old values don't load is the autocomplete="off". So I would like to know how to alert or get my oldvalue.
Set the values directly and use the default value if no old value is available.
value="{{ old('option' . $question->id, 1) }}"
value="{{ old('option' . $question->id, 0) }}"

can't get old() to work in laravel

in my blade i have 2 radio inputs
<form class="" action="{{route('admin.album.search')}}" method="post">
<input id="slug" type="radio" name="search" value="slug" >
<input id="id" type="radio" name="search" value="id">
in the same blade file I test to see old value of radio input
{{ old('search')}}
When I select first radio button and submit the form and page reloads I expect that old will be selected radio button value but nothing comes out. What could I be doing wrong?
Try this:
<form class="" action="{{route('admin.album.search')}}" method="post">
{{ csrf_field() }}
<input id="slug"
type="radio"
name="search"
value="slug"
{{ (old('search') == 'slug') ? 'checked': '' }}>
<input id="id"
type="radio"
name="search"
value="id"
{{ (old('search') == 'id') ? 'checked': '' }}>
N.T. If you submit post form request you must be specify csrf_field(). Otherwise it gives you token mismatch exception.

Laravel Blade template checkbox checked when another one is checked

I have the following checkboxes in view
<div class="form-group">
<label class="col-md-4 control-label">Roles</label>
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="data_entry"> Data Entry <br />
<input type="checkbox" name="save_on_ext_hd"> Save On External HD <br />
<input type="checkbox" name="print"> Print <br />
<input type="checkbox" name="export_csv">Export CSV <br />
<input type="checkbox" name="delete"> Delete
</label>
</div>
</div>
</div>
I am using laravel 5 and blade template.
the issue is: when click on any checkbox the first one checked and verse versa
Ex: when click pn print checkbox the data_entry checked automatically and when unchecked print data_entry unchecked.
also when check print data_entry checked and when check delete for example data_entry unchecked.
That's because all your checkboxes are inside one <label>. Labels with an input inside activate the input when clicked. So clicking anywhere inside the label will trigger all checkboxes.
Solve this by giving each its own <label>:
<label>
<input type="checkbox" name="data_entry"> Data Entry <br />
</label>
<label>
<input type="checkbox" name="save_on_ext_hd"> Save On External HD<br />
</label>
<!-- etc -->

Resources