Form Model Binding and Radio Buttons

I am building a form to edit a row with Form Model Binding, the problem is all radio buttons are unchecked, seems to don't read value of the database. I tried to do with if statement but throws a syntax error:

{{ Form::radio('tributary', 'true', @if ($station->tributary == true) true @else false @endif) }} Yes

Results in:

<?php echo Form::radio('tributary', 'true', <?php if($station->tributary == true): ?> true <?php else: ?> false <?php endif; ?>); ?> Yes

How can I fix it?

franzliedke replied 4 years ago

Try this:

{{ Form::radio('tributary', 'true', $station->tributary }} Yes
jsolivellase replied 4 years ago

franzliedke said:

Try this:

{{ Form::radio('tributary', 'true', $station->tributary }} Yes

Didn't work. Anyway it's strange, when tributary is false, both radio buttons (one for true value and another for false value) are unchecked but, when tributary is true, false radio button is checked.

jsolivellase replied 4 years ago Solution

Ok, I solved the issue, error was value of radio must be 1 or 0 (true and false didn't work for me) and not 'true' or 'false' (with '' are strings and not booleans). Now Form Modal Binding detects correctly the radio buttons.

{{ Form::radio('tributary', 1) }} Yes
{{ Form::radio('tributary', 0) }} No
scottycam replied 3 years ago

I found this page when looking for a solution to marking 1 of many radio buttons as true depending on what was saved in the database. I figured out a solution shortly after and thought I would add my solution if anyone else finds this page as I did. You can't use if statements to set values for the different options, plus this method is faster I believe.

{{ Form::radio('name', 'value1', ($var->option == 'value1') }} Value1
{{ Form::radio('name', 'value2', ($var->option == 'value2') }} Value2
{{ Form::radio('name', 'value'3, ($var->option == 'value3') }} Value3

this results in giving only the stored value a 1 which makes that option/value active.

JesusAlvSoto replied 3 years ago

The title says 'Form Model Binding and Radio Buttons' but I don't see any form model binding here! I was expecting to find some way of populating several radio buttons using form model binding...

jhourlad replied 3 years ago

I agree. No Form Model Binding on that solution.

joelfrens replied 2 years ago

I fixed it by using a ['checked' => 'checked'] array as a fourth argument in Laravel 5.3

{{ Form::label('visible', 'Visibility') }}

            @if ($category->visible == '1')
                {{ Form::radio('visible', '1', true, ['checked' => 'checked']) }} Yes
                <br />
                {{ Form::radio('visible', '0', false, []) }} No
                {{ Form::radio('visible', '1', false, []) }} Yes
                <br />
                {{ Form::radio('visible', '0', true, ['checked' => 'checked']) }} No

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us