2015-01-12 33 views
0

在我的更新配置文件表单中,我有一个标题选择列表。诚然,项目应从但是数据库暂时得到,我想这个特殊的选择列表中做到以下几点..Laravel更新表单 - 获取正确的选定状态

  1. 显示在初始页面加载所选项目。因此,如果$ user ['title']是'Mrs',则会显示。

  2. 如果laravel验证会在其他表单域中出现错误,并且用户已将标题更改为'Mr',则选定状态应该显示在'Mr'项上。

到目前为止我有以下内容,但它无法正常工作。

<select name="title" id="title"> 
<option value="Mr" @if(($user['title']) == 'Mr') selected @else @if((Input::old('title')) == 'Mr') selected @endif @endif>Mr</option> 
<option value="Mrs" @if(($user['title']) == 'Mrs') selected @else @if((Input::old('title')) == 'Mrs') selected @endif @endif>Mrs</option> 
.... 
</select> 

回答

0

您应该从Controller通为选择数据的同时USET的Form组件来构建选择在View,例如:

Controller方法:

$users = User::lists('title', 'id'); 
return View::make('user')->with('users', $user); 

View

// By default, item with id 1 (the first item) will be selected 
echo Form::select('title', $users, Input::old('title', 1), ['id'=>title]); 

如果您正在使用Blade,则:

// By default, item with id 1 (the first item) will be selected 
{{ Form::select('title', $users, Input::old('title', 1), ['id'=>title]) }} 

阅读documentation正常。您正在使用Laravel与旧学校PHP编码。

+0

我知道我正在使用旧学校的PHP和我承认在我的问题。不过,我仍然需要知道如何做到这一点。 – Suba