ducasse
125
5
Laravel

Handling unchecked checkbox with old()

Posted 1 year ago by ducasse

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 (as side note, is this correct or there's a better way?).

It seems that when checkbox is unchecked the relative old() doesn't exist.

I tried a lot of solutions I read here and on Stack but none works. Notice: I'm using Laravel 5.6

Please sign in or create an account to participate in this conversation.