使用 Laravel/Blade 中的旧输入定义选定的选项 [英] Define the selected option with the old input in Laravel / Blade

查看:11
本文介绍了使用 Laravel/Blade 中的旧输入定义选定的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have this code:

<select required="required" class="form-control" name="title">
    <option></option>
    @foreach ($titles as $key => $val)
        @if (stristr($key, 'isGroup'))
            <optgroup label="{{ $val }}">
        @else
        <option value="{{ $key }}">{{ $val }}</option>
        @endif
    @endforeach
    </select>

So when the form have errors i use the line Redirect::route('xpto')->withInput()->withErrors($v). But i can't re-populate the select fields. Any way to do this without using JavaScript for example?

解决方案

The solution is to compare Input::old() with the $keyvariable using Blade Directives - If Statements.

@if (Input::old('title') == $key)
    <option value="{{ $key }}" selected>{{ $val }}</option>
@else
    <option value="{{ $key }}">{{ $val }}</option>
@endif

这篇关于使用 Laravel/Blade 中的旧输入定义选定的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆