mleontenko
4 months ago
549
2
Laravel

Retain value of radio button, but set default value

Posted 4 months ago by mleontenko

I'm using old() to retain radio button selection after form validation error in Laravel, but I also want to make option "Unknown" selected by default.

The form element looks like this:

<label for="description">Product Available</label><br/>
<label class="radio-inline"><input type="radio" name="available" id="available" value="yes" {{ (old('available') == 'yes') ? 'checked' : ''}}>Yes</label>
<label class="radio-inline"><input type="radio" name="available" id="available" value="no" {{ (old('available') == 'no') ? 'checked' : ''}}> No</label>
<label class="radio-inline"><input type="radio" name="available" id="available" value="unknown" {{ (old('available') == 'unknown') ? 'checked' : ''}}> Unknown</label>

This works fine, but I need the "Unknown" option to be set by default when opening form without overriding "old()" data.

How can I do this?

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