【问题标题】:Laravel update form - get correct selected stateLaravel 更新表单 - 获得正确的选择状态
【发布时间】:2015-01-12 00:07:02
【问题描述】:

在我的更新个人资料表单中,我有一个标题选择列表。诚然,这些项目应该来自数据库,但是目前,我希望这个特定的选择列表执行以下操作..

  1. 在初始页面加载时显示所选项目。所以,如果 $user['title'] 是 'Mrs',就会显示出来。

  2. 如果 laravel 验证在其他表单字段上出现错误,并且用户已将标题更改为“先生”,则选定状态应为“先生”项。

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

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

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    您应该从Controller 传递选择的数据,并使用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 编码。

    【讨论】:

    • 我知道我使用的是老式 PHP,并且我在我的问题中承认了这一点。但是,我仍然需要知道如何做到这一点。
    猜你喜欢
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 2018-06-30
    • 2020-01-18
    相关资源
    最近更新 更多