Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ducasse's avatar

Handling unchecked checkbox with old()

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

0 likes
5 replies
jlrdw's avatar

If a checkbox is unchecked nothing is passed in post you have to test for that yourself and implement it is checked or unchecked.

Snapey's avatar
<input type="checkbox" name="member"
  @if (old('member',$user->member)) checked @endif  />

Old accepts a second parameter as the original value

Filip_Zdravkovic's avatar

@Snapey But that doesn't solve the problem. For example, let's say that $user->member is 1. When you open the edit form - it will be initially checked. But if you uncheck it, submit the form and get some validation error, then this:

<input type="checkbox" name="member"
  @if (old('member',$user->member)) checked @endif  />

... will not be unchecked. I think that's the problem @ducasse is having here.

This should work:

@if(! ($errors->any() && is_null(old('member'))) && old('member', $user->member)) checked @endif

... but I guess there's a better (more elegant) way ...

Snapey's avatar

3 months ago! I hope he is not stuck on the same problem @Filip_Zdravkovic

One option is a hidden checkbox field with the same name and a value of 0

<input type="hidden" value="0" name="member" />
<input type="checkbox" name="member"
  @if (old('member',$user->member)) checked @endif  />

This way, if the checkbox is unchecked, old() gets the hidden value instead.

1 like
Filip_Zdravkovic's avatar

3 months is nothing, time passes quickly ;-)

And thanks, that's a more elegant way.

Please or to participate in this conversation.