user3732216 user3732216 - 2 months ago 8
HTML Question

Radio Buttons and

I'm creating a form for a page with my Laravel app and I want to know if anyone can tell me how I can fix this so that when my form loads up there isn't any options preselected because for some reason when I go to create a new entity on the create form there’s already selection made.

<div class="radio-custom radio-primary radio-inline">
<input id="active" type="radio" name="is_active" {{ (old('is_active') == NULL || ((isset($title) && $title->is_active == 1))) ? 'checked="checked"' : '' }} value="1">
<label for="active">Active</label>
</div>
<div class="radio-custom radio-primary radio-inline">
<input id="active" type="radio" name="is_active" {{ (old('is_active') == NULL || ((isset($title) && $title->is_active == 0))) ? 'checked="checked"' : '' }} value="0">
<label for="inactive">Inactive</label>
</div>

Answer

This should solve your problem. You have an logical error within your if/else statement. Whenever there is no old value you try to check both radio buttons.

<div class="radio-custom radio-primary radio-inline">
    <input id="active" type="radio" name="is_active" {{ (old('is_active') !== NULL || ((isset($title) && $title->is_active == 1)))  ? 'checked="checked"' : '' }} value="1">
    <label for="active">Active</label>
</div>
<div class="radio-custom radio-primary radio-inline">
    <input id="active" type="radio" name="is_active" {{ (old('is_active') !== NULL || ((isset($title) && $title->is_active == 0)))  ? 'checked="checked"' : '' }} value="0">
    <label for="inactive">Inactive</label>
</div>

This should work as well:

<div class="radio-custom radio-primary radio-inline">
    <input id="active" type="radio" name="is_active" {{ (isset($title) && $title->is_active == 1)  ? 'checked="checked"' : '' }} value="1">
    <label for="active">Active</label>
</div>
<div class="radio-custom radio-primary radio-inline">
    <input id="active" type="radio" name="is_active" {{  (isset($title) && $title->is_active == 0)  ? 'checked="checked"' : '' }} value="0">
    <label for="inactive">Inactive</label>
</div>

Hope this solved your problem. :) Highly prefer the second solution since the first one is just unnecessary code.