[英]Laravel: How to Populate Form with Values from Session
我得到了這個將值存儲到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 :: model()替換Form :: open()
我的表單剛剛中斷,表單也沒有填充。
以下是我的會話中存儲的內容:
{"_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模型。 它也可以使用標准對象或關聯數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.