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

coffee's avatar

How to use old() function with radio button

I have arrays of radio named "radio[]" with 5 options to choose from. I need to retain the inputed data when validation fails. How do we use old() function with radio buttons?

<input type="radio" id="s10" name="radio[1]" value="5" /><label for="s10" title="5">5</label>
<input type="radio" id="s11" name="radio[1]" value="4" /><label for="s11" title="4">4</label>
...
0 likes
18 replies
nicoqh's avatar

You should share it with the other readers when you find a solution to your own problem.

6 likes
coffee's avatar
coffee
OP
Best Answer
Level 5
<input type="radio" id="s10" name="radio[1]" value="5"  { { old('radio.1')=="5" ? 'checked='.'"'.'checked'.'"' : '' } } /><label for="s10" title="5">5</label>

Maybe someone will rewrite it and even simplify it.

9 likes
grim359's avatar

<input type="radio" id="s10" name="radio[1]" value="5" { { old('radio.1')=="5" ? 'checked='.'"checked"' : '' } } /><label for="s10" title="5">5</label>

You could just put the double quotes inside of the second "checked" to cut down a couple dots.

Cronix's avatar

@grim359 actually you can just use checked with HTML5. You don't need the old style checked="checked". <input type="radio" checked> The same goes for selected in <select> options, <option value="1" selected>value</option>

1 like
Steveoo's avatar

Thanks! I was missing the dot notation on the named array and couldn't get the value to show.

bdjunayed's avatar

2020 update!

<input type="checkbox" name="ielts" value="IELTS" {{ old('ielts') == "IELTS" ? 'checked' : '' }}>

Or

<option value="2" {{ old('experience_three') == 2 ? 'selected' : '' }}>2 Years</option>
9 likes
zrosen's avatar

Hi all - related to this, curious to your thoughts when the values of the radio buttons are 0, 1, 2, 3, etc. By default, this code will assume 0=false and 1=true, and therefore if there are multiple it will assume 0 means nothing instead of the first radio button. My solution was the following - does anyone have a better or more efficient way to do this?

<input type="radio" name="contact-type" value="{{ $type->id }}" {{ old('contact-type') !== null && old('contact-type') == $type->id ? 'checked' : '' }}>

Thanks!

Please or to participate in this conversation.