Laravel 4 - Input::old 用于单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17059244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:56:33  来源:igfitidea点击:

Laravel 4 - Input::old for radio buttons

laravellaravel-4

提问by BigJobbies

I was wondering if some Laravel guys can help out.

我想知道是否有一些 Laravel 人可以提供帮助。

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.

我有一个表单,其中有 2 个单选按钮,当表单提交时,它会通过验证器,如果验证器失败,它会返回到表单,使用输入填充字段并显示错误消息。

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.

任何帮助将不胜感激。

回答by Melvin

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

您可以使用 Laravel 的开箱即用的 HTML 收音机来尝试此操作... Laravel Docs 表单复选框和收音机

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,

或者只是 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'));

回答by JasonMortonNZ

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"; } ?> >

回答by thujohn

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

该错误是已知的: - https://github.com/laravel/laravel/issues/2069- https://github.com/laravel/framework/issues/1564

You have a temporary solution in the second link.

您在第二个链接中有一个临时解决方案。

回答by Pedro Moreira

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 类上创建了一个函数。

Helper.php:

帮手.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.

每个选项都将其名称和值传递给辅助函数,并且先前选择的选项将打印“已检查”。此外,一个选项可以传递 'true' 作为第三个参数,因此如果没有旧输入,它就会被选中。

回答by Ricardo Canelas

Using with the Bootstrap & Automatic checking

与 Bootstrap 和自动检查一起使用

Add this code at end of file:app/start/global.php

在文件末尾添加此代码:app/start/global.php

//...

Form::macro('radio2', function($group ='group-name', $value_model = 'value-model', $label ='label-radio', $value_radio = 'value-radio', $attrs = array())
{

  $item  = "<div class='radio'>";
  $item .=   "<label>";
  $item .=      Form::radio($group, $value_radio, ($value_model == $value_radio) ? true : false, $attrs);
  $item .=      $label;
  $item .=   "</label>";
  $item .= "</div>";

  return $item;
});

In your view.php

在你看来.php

{{ Form::radio2('status', Input::old('status'), 'Online', '1', array()) }}
{{ Form::radio2('status', Input::old('status'), 'Offline', '0', array()) }}

Final result:

最后结果:

enter image description here

在此处输入图片说明

回答by roberto

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

我尝试简单地使用 Input::get() 而不是 Input::old(.... 并且它奏效了!!{{Form::radio('estado','V',(Input::get('estado')=="V"))}}

回答by Kronzeuge

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.

我的方法是带有刀片语法的嵌套速记 if/else 语句。此解决方案还考虑了在数据库中设置的初始值。

<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.

使用 Laravel 5.7 测试。