Laravel 5.1 how to use {{ old('') }} helper on blade file for radio inputs
Asked Answered
K

5

15

I'm currently learning laravel and creating my first form. Everything is awesome until I want to use {{ old('') }} helper in my blade file for radio buttons. I'm not sure how to go about doing it properly, and I can't seem to find much info on here about it either.

The code I have is as follows:

<div class="form-group">
    <label for="geckoHatchling">Gecko Hatchling?</label>
    <div class="radio">
        <label>
            <input type="radio" name="geckoHatchling" id="geckoHatchlingYes" value="1">
            Yes
        </label>
    </div>
    <div class="radio">
        <label>
            <input type="radio" name="geckoHatchling" id="geckoHatchlingNo" value="0" checked>
            No
        </label>
    </div>
</div>
Kura answered 15/10, 2015 at 6:7 Comment(0)
H
28

I think the following is a little bit cleaner:

<input type="radio" name="geckoHatchling" value="1" @if(old('geckoHatchling')) checked @endif>

<input type="radio" name="geckoHatchling" value="0" @if(!old('geckoHatchling')) checked @endif>

@if is checking the truthiness of the old value and outputting checked in either case.

Handbook answered 15/10, 2015 at 7:22 Comment(3)
Thank you for your answer, can you explain the differences between those 2 lines? I see you added !old but unsure on how this works?Kura
Adding a ! is like saying not in PHP. So the line reads: If the value of old() is false (so a zero = not checked), output the checked attribute.Handbook
Shouldn't the second line be id="geckoHatchlingNo" value="0" @if(!old...Tolbutamide
S
13

You can use Form-helper, but it doesn't come out of the box with Laravel. You should install it manually. Please read the docs.

WITH FORM-HELPER

1. Blade

{!! Form::radio('geckoHatchling', '1', (Input::old('geckoHatchling') == '1'), array('id'=>'geckoHatchlingYes', 'class'=>'radio')) !!}
{!! Form::radio('geckoHatchling', '0', (Input::old('geckoHatchling') == '0'), array('id'=>'geckoHatchlingNo', 'class'=>'radio')) !!}

2. PHP

echo Form::radio('geckoHatchling', '1', (Input::old('geckoHatchling') == '1'), array('id'=>'geckoHatchlingYes', 'class'=>'radio'));
echo Form::radio('geckoHatchling', '0', (Input::old('geckoHatchling') == '0'), array('id'=>'geckoHatchlingNo', 'class'=>'radio'));

WITHOUT FORM-HELPER

1. Blade

<input type="radio" name="geckoHatchling" id="geckoHatchlingYes" value="1" @if(Input::old('geckoHatchling')) checked @endif>
<input type="radio" name="geckoHatchling" id="geckoHatchlingNo" value="0" @if(!Input::old('geckoHatchling')) checked @endif>

2. PHP

<input type="radio" name="geckoHatchling" value="1" class="radio" id="geckoHatchlingYes" <?php if(Input::old('geckoHatchling')== "1") { echo 'checked'; } ?> >
<input type="radio" name="geckoHatchling" value="0" class="radio" id="geckoHatchlingNo" <?php if(Input::old('geckoHatchling')== "0") { echo 'checked'; } ?> >
Suppuration answered 15/10, 2015 at 6:27 Comment(1)
Please don't forget that the Form-Helper doesn't come out of the box with Laravel anymore. If you want to use it you have to install it manually as described here: laravelcollective.com/docs/5.0/htmlHandbook
S
6

I have tried the accepted answer's solution, it doesn't work for me,

I had to use this {{}} curly braces (which is blade template's echo) in order to use the conditional statement.

<input type="radio" name="geckoHatchling" id="geckoHatchlingYes" value="1" {{(old('geckoHatchling') == '1') ? 'checked' : ''}}>
Skell answered 5/7, 2018 at 13:26 Comment(3)
While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked.Capone
This is just a shorter version of the correct answer, so yeah it is a viable option but with how Laravel has progressed in the 3 years since I asked this question the documentation has made this clearer :)Kura
@AndyHolmes when i googled for How to use {{old('')}} on radio buttons it resulted ur question first. Laravel doesn't have any document that explains how to use old() on radio buttons. Hope it helps for some beginners.Skell
C
1

I have stumbled across what I believe will be to be a more accurate and comprehensive answer to this question. The problem with the main answer provided is the last radio button will always be selected moving forward. However, I have provided a code example below to show how to resolve this issue. (PS - this works in Laravel 8, and, I'm only assuming it will work for previous versions)

<div> <input type="radio" name="role" value="teacher" @if(old('role') == 'teacher') checked @endif> </div>
        
<div> <input type="radio" name="role" value="student" @if(old('role') == 'student') checked @endif> </div>
        
<div> <input type="radio" name="role" value="assistant" @if(old('role') == 'assistant') checked @endif> </div>

Just using:

@if(old('role') checked @endif

will always result in your last radio button being selected if you have multiple radio buttons.

Consuetudinary answered 18/1, 2021 at 13:5 Comment(1)
Yeah this is a fine solution for your scenario where you're matching actual values, the question is based on true/false so the answer I selected is correct for my use case. Your answer is also absolutely spot on for anyone using string valuesKura
S
1

befor Laravel 9

<input type"radio" name="active" value="1" {{old('active',user->active))?'checked',''}}/>
<input type"radio" name="active" value="0" {{old('active',user->active))?'','checked'}}/>

Laravel 9

<input type"radio" name="active" value="1" @checked(old('active',$user->active))/>
<input type"radio" name="active" value="0" @checked(!old('active',$user->active))/>
Scallop answered 19/2, 2022 at 10:29 Comment(1)
This could arguably be achieved prior to Laravel 9 by using a custom blade directive, but good update :)Kura

© 2022 - 2024 — McMap. All rights reserved.