【问题标题】:Laravel 4 - Input::old for radio buttonsLaravel 4 - 单选按钮的 Input::old
【发布时间】:2013-06-08 04:42:32
【问题描述】:

我想知道是否有一些 Laravel 人员可以帮忙。

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

我似乎无法为单选按钮执行此操作,如果在提交表单时单击了一个并且出现错误,它会返回到表单并填写所有内容,除了已检查的单选按钮现在为空。

我的单选按钮如下:

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

任何帮助将不胜感激。

【问题讨论】:

    标签: laravel laravel-4


    【解决方案1】:

    您可以使用 Laravel 开箱即用的 HTML 收音机来尝试这个... Laravel Docs Form checkboxes and Radio

    使用刀片,

    {{ 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')) }}
    

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

    【讨论】:

    • 我看到这个“方法收音机不存在”。当我使用这个
    • 似乎有问题的代码没有使用刀片模板或 laravel HTML / Form helpers
    • 哎呀......只是建议......因为他提到了laravel......干杯
    • @BigJobbies 刚刚编辑它.. 使用 Form::radio() 而不是 HTML::radio().. 我的错..
    • +1 这里不需要三元,只需传入Input::old('genre') == 'M'作为参数值。
    【解决方案2】:

    你可以这样做:

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

    【讨论】:

    • 嘿,默认情况下会检查男性单选按钮,但不是吗?
    • 是的,你可以切换它,让女性成为默认值。但是,如果您愿意,可以在两个输入字段中添加 if 语句。
    • 您不希望默认选择任何无线电输入吗?我更新了我的代码,所以默认情况下不检查任何输入。
    【解决方案3】:

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

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

    【讨论】:

    • 这并不能真正回答所提出的问题。
    • 你说得对,这并没有真正回答,但我希望它会尽快修复:)
    【解决方案4】:

    我刚刚偶然发现这一点,我不想在每个表单上都重复这样的条件,所以我在我的 Helper 类上创建了一个函数。

    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' : '');
        }
    }
    

    所以,现在在我的表单上,我只是这样使用它:

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

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

    【讨论】:

      【解决方案5】:

      与引导程序和自动检查一起使用

      在文件末尾添加此代码: 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;
      });
      

      在你的 view.php 中

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

      最终结果:

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

          我的方法是使用刀片语法的嵌套速记 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>
          

          用 Laravel 5.7 测试。

          【讨论】:

            猜你喜欢
            • 2015-07-15
            • 2016-10-09
            • 2015-09-17
            • 2016-02-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-10-16
            • 2013-05-28
            相关资源
            最近更新 更多