Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 678
Andy Holmes Avatar asked Oct 15 '15 06:10

Andy Holmes


1 Answers

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.

like image 54
Tim Avatar answered Oct 17 '22 21:10

Tim