Laravel can't uncheck checkbox using old with default value set - laravel

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' : '' }}>

Related

Laravel: keep the checkbox selection after a validation error?

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' : '' }}>

Livewire modal window with readonly inputs

I'm using Jetstream blade components in my project, including x-jet-dialog-modal and x-jet-input. The modal window is used to add or edit records and works fine. The inputs are bound to a model "person" using the "wire:" syntax, and everything goes as expected.
Now I want to use the same modal window to show the record fields in a read-only manner, when pressing a "view" button. My idea is to make the inputs read only and hide the "save" button dynamically, using a public property of the Livewire controller (component).
So, in Livewire component I have a default value:
public $disableEdition = false;
And in the blade file:
<div class="mt-2">
<x-jet-label for="name" value="{{ __('Name') }}" />
<x-jet-input id="name" type="text" class="form-control" wire:model.defer="person.name" {{ $this->disableEdition ? 'readonly' : '' }}/>
<x-jet-input-error for="person.name" class="mt-2" />
</div>
I expected the input field to appear with attribute "readonly" (and of course non-editable and formatted with corresponding Bootstrap styles), but the browser inspector reveals that no attribute was added to the input.
Maybe you can help me with a solution or even a better approach to accomplish my goal.
Best regards.

Laravel - Is there any better approach to check the checkbox with the user previous input value in blade?

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',
]);
}

Laravel validator on a form and send post

I used laravel validator.
I have a form wih 2 fields
If i fill one field "NAME" and i leave the other "SURNAME" empty pressing SAVE button of the form appears required alert message on SURNAME but the name inserted in the NAME field became empty! And i think form send data anyway.why?I aspect that form doesn't send datas
In the value attribute of each input you need to echo old("the-input-name") so it can retain the previous input before the error
<input name="name" value="{{old('name')}}" class="" />
You should use old('NAME') to get the value of the field after the validation fails, blade example:
<input type="text" value="{{ old('NAME') }}" />
An example In case of update forms you should display the original value from the database unless there's an error:
<input type="text" value="{{ old('NAME') ?? $your_entry->value }}" />
Take a look at the documentation for more details

Edit Form (Laravel 5.3)

I am using same form for Create and Edit i have a Folder field and i want to set its value Inbox by default however if a user want to replace it with own folder he /she can replace
<input type="text" name="folder" value="INBOX {{isset($mbl->folder) ? $mbl->folder : '' }}" class="form-control" />
when i go on edit it shown **INBOX INBOX
I want that Default value should not be show in Edit form it just show the name from Folder remember that i can't remove value="INBOX" because i need it i am using it as default value and i am using same form for Create and Edit
Thanks For Help
You Need to pass default value as
<input type="text" name="folder" value="{{isset($mbl->folder) ? $mbl->folder : 'INBOX' }}" class="form-control" />
Try This hope it will work
Simply use it as follows:
<input type="text" name="folder" value="{{$mbl->folder or 'INBOX' }}" class="form-control" />
More info here.

Resources