Laravel 4 - Input::old for radio buttons

2020-07-10 09:11发布

I was wondering if some Laravel guys can help out.

I have a form in which i have 2 radio buttons, when the form submits it goes through the validator, if the validator fails it comes back to the form, populates the fields with the input and displays error messages.

I cant seem to do this for radio buttons, if one is clicked when the form is submitted and there was an error, it comes back to the form with everything filled out EXCEPT the radio button that was checked is now empty.

My radio buttons are as follows:

<input type="radio" name="genre" value="M" class="radio" id="male" />
<input type="radio" name="genre" value="F" class="radio" id="female" />
<span class="error">{{ $errors->first('genre') }}</span>

Any help would be greatly appreciated.

7条回答
地球回转人心会变
2楼-- · 2020-07-10 09:36

I tried simply using Input::get() instead of Input::old().... and it's worked!!{{Form::radio('estado','V',(Input::get('estado')=="V"))}}

查看更多
叛逆
3楼-- · 2020-07-10 09:39

The bug is known : - https://github.com/laravel/laravel/issues/2069 - https://github.com/laravel/framework/issues/1564

You have a temporary solution in the second link.

查看更多
Summer. ? 凉城
4楼-- · 2020-07-10 09:48

I've just stumbled into this and I don't want to keep repeating such conditions on every form, so I've created a function on my Helper class.

Helper.php:

class Helper {

    // other functions

    public static function oldRadio($name, $value, $default = false) {
        if(empty($name) || empty($value) || !is_bool($default))
            return '';

        if(null !== Input::old($name)) {
            if(Input::old($name) == $value) {
                return 'checked';
            } else {
                return '';
            }
        } else {
            if($default) {
                return 'checked';
            } else {
                return '';
            }
        }

        // Or, short version:
        return null !== Input::old($name) ? (Input::old($name) == $value ? 'checked' : '') : ($default ? 'checked' : '');
    }
}

So, now on my forms, I just use it like this:

<label>Please select whatever you want</label>
<div class="radio-inline"><label><input type="radio" name="whatever" value="1" required {{ Helper::oldRadio('whatever', '1', true) }}> One</label></div>
<div class="radio-inline"><label><input type="radio" name="whatever" value="2" {{ Helper::oldRadio('whatever', '2') }}> Two</label></div>
<div class="radio-inline"><label><input type="radio" name="whatever" value="3" {{ Helper::oldRadio('whatever', '3') }}> Three</label></div>

Each option passes its name and value to the helper function and the previously selected one will print 'checked'. Additionally, an option can pass 'true' as the third parameter so it gets selected if there was no old input.

查看更多
▲ chillily
5楼-- · 2020-07-10 09:54

My approach is a nested shorthand if/else statement with blade syntax. This solution considers also the initial value that is set in the database.

<div class="form-check form-check-inline">
    <input class="form-check-input" type="radio" name="sex" id="male" value="male"{!! ((old('sex') == 'male') ? ' checked="checked"' : ((empty(old('sex')) && $user->sex == 'male') ? ' checked="checked"' : '')) !!}/>
    <label class="form-check-label" for="male">Männlich</label>
</div>
<div class="form-check form-check-inline">
    <input class="form-check-input" type="radio" name="sex" id="female" value="female"{!! ((old('sex') == 'female') ? ' checked="checked"' : ((empty(old('sex')) && $user->sex == 'female') ? ' checked="checked"' : '')) !!}/>
    <label class="form-check-label" for="female">Weiblich</label>
</div>

Tested with Laravel 5.7.

查看更多
贼婆χ
6楼-- · 2020-07-10 09:56

You could do this:

<input type="radio" name="genre" value="M" class="radio" id="male" <?php if(Input::old('genre')== "M") { echo 'checked="checked"'; } ?> >
<input type="radio" name="genre" value="F" class="radio" id="female" <?php if(Input::old('genre')== "F") { echo 'checked="checked"; } ?> >
查看更多
Melony?
7楼-- · 2020-07-10 09:58

You can try this using Laravel's out of the box HTML radio... Laravel Docs Form checkboxes and Radio

Using blade,

{{ Form::radio('genre', 'M', (Input::old('genre') == 'M'), array('id'=>'male', 'class'=>'radio')) }}
{{ Form::radio('genre', 'F', (Input::old('genre') == 'F'), array('id'=>'female', 'class'=>'radio')) }}

Or just php,

echo Form::radio('genre', 'M', (Input::old('genre') == 'M'), array('id'=>'male', 'class'=>'radio'));
echo Form::radio('genre', 'F', (Input::old('genre') == 'F'), array('id'=>'female', 'class'=>'radio'));
查看更多
登录 后发表回答