Laravel:如何使用会话中的值填充表单
Laravel: How to Populate Form with Values from 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
但每当我只是更换
Form::open()
和
Form::model()
我的表格刚刚损坏,也没有填充表格。
这是我的会话中存储的内容:
{"_token":"iThJYWxZj3APr7Unzrczd4WrGfs6dGsUtKINyTIC","_previous":{"url":"http:\/\/uploader.dev"},"flash":{"old":[],"new":[]},"householdCount":"2","city":"adklf","comment":"akldfjlakdjf","comments":"lkjaldfj"}
那么,如何使用存储在该会话中的值填充我的表单?
检索您的会话数据并将其填充到关联数组中。然后,将该数组传递给 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 模型。它也可以使用标准对象或关联数组。
我得到了这个将值存储到会话中的表单,因为它是多步骤过程的一部分。单击下一步时,值将存储在会话中,您将重定向到步骤 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
但每当我只是更换 Form::open() 和 Form::model()
我的表格刚刚损坏,也没有填充表格。
这是我的会话中存储的内容:
{"_token":"iThJYWxZj3APr7Unzrczd4WrGfs6dGsUtKINyTIC","_previous":{"url":"http:\/\/uploader.dev"},"flash":{"old":[],"new":[]},"householdCount":"2","city":"adklf","comment":"akldfjlakdjf","comments":"lkjaldfj"}
那么,如何使用存储在该会话中的值填充我的表单?
检索您的会话数据并将其填充到关联数组中。然后,将该数组传递给 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 模型。它也可以使用标准对象或关联数组。