Reputation: 13910
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
Upvotes: 4
Views: 914
Reputation: 1038
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 />
Upvotes: 3
Reputation: 2588
Change it to
<input type="checkbox" name="member" {{ (($mode == 'edit' && $user->member == 1) || old('member')) ? 'checked' : null }} />
Upvotes: 0