我在想,如果一些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>
任何帮助将不胜感激。
你可以这样做:
<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"; } ?> >
你可以试试这个使用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'));
是已知的bug: - https://github.com/laravel/laravel/issues/2069 - https://github.com/laravel/framework/issues/1564
你必须在第二个环节是治标不治本。
我只是偶然到这一点,我不想让每个表单上重复这样的条件,所以我创建了我的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>
每个选项通过其名称和值的辅助功能和先前选择一个将打印“检查”。此外,所以如果没有旧的输入被选中的选项可以通过“真实”作为第三个参数。
自举和自动检查使用
在文件末尾添加以下代码:应用程序/启动/ 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()) }}
最后结果:
我想简单地使用输入:: get()方法,而不是输入::旧的()......和它的工作!{{Form::radio('estado','V',(Input::get('estado')=="V"))}}
我的做法是嵌套的简写,如果/与刀片语法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。