In my form I want the checkboxes to keep their states (checked or unchecked) after a validation error.
This is what I have so far:
<input type="checkbox" id="delete-user" class="form-check-input" data-data-section="users" name="permissions[]" value="delete-user" {{ (is_array(old('permissions')) && in_array(1, old('permissions'))) ? ' checked' : '' }}>
But it looks that the old() function doesn't work.
I also tried:
old('permissions.'.$key)
where $key is the checkbox array index but it doesn't work neither.
Any idea ?
I think you are an invalid value pass in the in_array function
Try this:
value="delete-user" {{ (is_array(old('permissions')) && in_array('delete-user', old('permissions'))) ? ' checked' : '' }}>
Related
I'm learing laravel and have some checkboxes on my form. However by default they have the value of on and off -> null or not passed.
But in my tables are using a boolean of 0 and 1. I've covered for this in my controller which takes the on or off values and puts them to 0 or 1 .
HotelFacility::create([
'hotel_id' => $hotel->id,
'fitness_centre' => $request->has('fitness_centre') ? 1 : 0,
'bar' => $request->has('bar') ? 1 : 0,
]);
But how do I now cover for this in my blade template as it's now reading back the opposite?
<input type="checkbox" name="fitness_centre" value="{{ $hotel[0]->hotelFacilites->fitness_centre ? 'on' : 'off' }}">
Fitness Centre
</label>
Please try this because if you want to check the checkbox as per database value so you need to put checked not value.
<input type="checkbox" name="fitness_centre" {{ $hotel[0]->hotelFacilites->fitness_centre ? ' checked ':''}}>
I have a checkbox
<input type="checkbox" name="example">
The user submits the form and return back due to failed validation so I want to show the previous state of the checkbox
<input
type="checkbox"
name="example"
{!! old('example') ? 'value="1" checked' : 'value="0"' !!}>
First question here is that, what to do with validation now? if it was 0 or 1 I would add boolean validation but now the first time it is on and after that it is 0 or 1.
Someone may say if you use that {!! old('example') ? 'value="1" checked' : 'value="0"' !!} initially, its value won't be on anymore.
So my first question changes to this: Initially the value of old('example') is null so the checkbox doesn't have checked attribute and the value of checkbox would be 0. Now when the user submits the form the old('example') has value (IT IS NOT NULL) and so this time the the checkbox has checked attribute and its value is 1 while it mustn't.
========
Another issue I encounter with is that, if old('example') has true value (which means the user had checked the checkbox), I should put something to check the checkbox. But it is not finished yet. I want to check the value of example that is in database so if the old('example') has false value(which means the user hadn't checked the checkbox) I want to check the value of example, if its value is 1 to check the checkbox and set the value of checkbox to 1 else 0. So what comes to my mind is this:
<input
type="checkbox"
name="example"
value="{!! old('example', isset($collection) ? $collection->example : null) == '1' : '1' : '0' !!}">
Now the problem is that old('example') would be on or NOTHING. And the value of $collection->example would be 1 or 0 or null. So if I compare it with '1' it won't be true even if the old('example') is true and similarly if I compare it with on, it won't be true even if the value of $collection->example is true. So let's do this:
#if(old('example') == 'on' || (isset($collection) && $collection->example))
<input type="checkbox" name="example" value="on" checked="checked">
#else
<input type="checkbox" name="example">
#endif
by this code above the issue is fixed. But is there any other better approach?
So my question is what to do with the validation in the back-end and of course what to do with check-boxes in front-end in an better approach without getting involved in jQuery and such a like?
I think it will solve your problem give it a try.
Blade file:
<input type="checkbox" name="example" value="{{ old('example') ?? '1' }}"
{{ old('example') == '1' ? 'checked' : null }} required>
The required will force the user to check the checkbox on the frontend, and on the backend, you can use the required validation because we can't trust the frontend validation.
Controller:
public function store(Request $request)
{
$request->validate([
'example' => 'required',
]);
}
I have a very simple checkbox in my application form
<input type="checkbox" name="active" {{ ( empty(old('active')) ? '' : ' checked' ) }}>
My requirements are:
the checkbox should be checked by default when the user arrives on the site for the first time
the checkbox should keep its value after the form validation fails
To me, it seems impossible to achieve this simply by using old as it will either
(2) remember the value after validation fails but it will not be checked by default at the beginning
(1) be checked by default, e.g. old('active', true) but then it will not be possible to uncheck it (i.e. unchecked box will be checked after validation fails because the default value is set in old).
What is the standard way of dealing with this? Or is there no way around using another field to check if the form was submitted?
<input type="checkbox" name="active" {{ ( empty(old('active')) && !empty(old('submit')) ? '' : ' checked' ) }}>
<input type="hidden" name="submit" value="submit">
Thank you
This maybe dirty but you can append empty string if the $request->active is null.
The code is something like this $request->active ?? '';.
You can pass the default value into old helper.
Try this:
<input type="checkbox" name="active" {{ old('active', true) ? 'checked' : '' }}>
When I uncheck a checkbox and validation fails I expect that checkbox remains unchecked, instead it is checked.
My checkbox:
<input type="checkbox" name="member"
{{ ($mode == 'edit' && $user->member == 1) ? 'checked' : '' }}
{{ (old('member') == 'on') ? 'checked' : '' }} />
Where $mode == 'edit' is passed from controller to indentify the case when I'm editing the form and then to populate form fields.
It seems that when checkbox is unchecked the relative old() doesn't exist.
I tried a lot of solutions here on Stack but none works. Notice: I'm using Laravel 5.6
I've solved it by testing the existence of old('_token') (the CSRF token) as follows:
<input type="checkbox" name="member"
#if ((!old('_token') && $mode == 'edit' && $user->member == 1) || (old('member') == 'on'))
checked
#endif />
Change it to
<input type="checkbox" name="member" {{ (($mode == 'edit' && $user->member == 1) || old('member')) ? 'checked' : null }} />
I have a form with a couple of radio buttons;
<div class="form-group{{ $errors->has('procurement') ? ' has-error' : '' }} col-md-6">
<label for="procurement" class="col-md-6 control-label">Procurement Type <span class="red">*</span></label><br>
<div class="col-md-12">
<input id="procurement" type="radio" name="procurement" value="owned"> Owned
<input id="procurement" type="radio" name="procurement" value="rental"> Rental
#if ($errors->has('procurement'))
<span class="help-block">
<strong>{{ $errors->first('procurement') }}</strong>
</span>
#endif
</div>
I am reusing the form for editing purposes so I want to be able to bind the object's value for 'procurement' when I present the form in edit view. I am able to use this bind the values for text inputs;
value="{{ isset($vehicle->model) ? $vehicle->model : old('model') }}"
But this does not work for radios or selects. What should I be doing? I am NOT using the Form facade for this.
Thanks!
You can use selected and checked for what you want to do. Just add it to the end of the element, and it will select/check the element.
Something like this for input type radio:
<input id="procurement" type="radio" name="procurement" value="owned" {{ old('model') === "owned" ? 'checked' : (isset($vehicle->model) && $vehicle->model === 'owned' ? 'checked' : '') }}> Owned
<input id="procurement" type="radio" name="procurement" value="rental" {{ old('model') === "owned" ? 'checked' : (isset($vehicle->model) && $vehicle->model === 'rental' ? 'checked' : '') }}> Rental
And this for select options:
<option value="something" {{ old('model') === "something" ? 'selected' : (isset($vehicle->model) && $vehicle->model === 'owned' ? 'selected: '')}}>Something</option>
Side note: I'd recommend making sure that old('something') comes before other possible values when you're filling values in. For instance, you had:
value="{{ isset($vehicle->model) ? $vehicle->model : old('model') }}"
which would override the user's original input with database results (if any, of course) upon a failed submission. So if I was say, updating my name in a text field, and there were errors to the form, it would direct me back to the form and lose my input because the database results are before the old value. Hope that makes sense!