Laravel:如何使用Session中的值填充表单

时间:2015-02-19 18:25:27

标签: php forms session laravel

我得到了这个将值存储到Session中的表单,因为它是多步骤进程的一部分。单击下一步时,值将存储在会话中,您将重定向到步骤2.

然而,在第2步中,您可以选择返回步骤1进行更正。现在我想用已存储在会话中的值填充步骤1的表单,以便用户不必重写所有内容。

以下是第1步的表格:

    {!! Form::open(['route' => 'orders.store', 'data-toggle' => 'validator', 'class' => 'form-horizontal']) !!}

        <div class="form-group">
            {!! Form::label('household_count', 'What is the householdCount?', ['class' => 'col-sm-2']) !!}
            <div class="col-sm-10"> 
                <label class="radio-inline">
                {!! Form::radio('householdCount', '1', false, ['required' => 'required']) !!} 1
                </label>

                <label class="radio-inline">
                {!! Form::radio('householdCount', '2', false, ['required' => 'required']) !!} 2
                </label>

            </div>
        </div>

        <div class="form-group">
            {!! Form::label('city', 'Where do you live?', ['class' => 'col-sm-2']) !!}
            <div class="col-sm-10"> 
                {!! Form::text('city', null, ['class' => 'form-control', 'placeholder' => 'e.g. Berlin', 'required']) !!}
                <div class="help-block with-errors"></div>
            </div>
        </div>


        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                {!! Form::submit('Next', ['class' => 'btn btn-primary btn-next']) !!}
            </div>
        </div>

    {!! Form::close() !!}

这是创建表单的方法。

public function step1() {
    return view('step1.create');
}

我已经找到了这个:http://laravel.com/docs/5.0/html#form-model-binding

但每当我换下来的时候      表::打开() 同      形式::模型()

我的表单中断,表单也没有填充。

以下是我的会话中存储的内容:

{"_token":"iThJYWxZj3APr7Unzrczd4WrGfs6dGsUtKINyTIC","_previous":{"url":"http:\/\/uploader.dev"},"flash":{"old":[],"new":[]},"householdCount":"2","city":"adklf","comment":"akldfjlakdjf","comments":"lkjaldfj"}

那么,如何使用存储在此会话中的值填充表单?

1 个答案:

答案 0 :(得分:3)

检索会话数据并将其填充到关联数组中。然后,将该数组传递给Form::model()标记。

// Populate your array in the controller.
$data = [
  'name' => 'Cryode',
  'age'  => 29,
];

return View::make('myForm', ['modelData' => $data]);

// Use Form::model() in your view.
{{ Form::model($modelData, [ ... ]) }}

Form::model()不需要Eloquent模型。它也可以使用标准对象或关联数组。