沒有Laravel這樣做的方式,但是您可以使用HTML基本表單數組來使其工作。您需要了解您必須識別您的表單和字段,以便Laravel確切知道數據來自何處以及將數據發送回的位置。如果你的所有領域都有相同的名字,它怎麼可能知道?
這是從您的routes.php
文件直接工作的概念證明。
正如我所做的那樣,在發佈答案之前,我使用了Route::get()
和Route::post()
,不必創建控制器和視圖來測試我不會使用的東西。在開發這個過程中,你必須把這個邏輯放在一個控制器和一個視圖中,我認爲它們是可信的。
要測試它的方式,你只需要將瀏覽器指向以下路徑:
http://yourserver/form
,當你按下一個按鈕,它會自動發佈壽路線:
http://yourserver/post
我基本上放棄一切形式的一個號碼,並給予按鈕我們將在Laravel中使用的數字來獲取表單數據並進行驗證。
Route::get('form', function()
{
return Form::open(array('url' => URL::to('post'))).
Form::text('form[1][name]', null).
Form::email('form[1][email]', null).
'<button type="submit" name="button" value="1">submit</button>'.
Form::close().
Form::open(array('url' => URL::to('post'))).
Form::text('form[2][name]', null).
Form::email('form[2][email]', null).
'<button type="submit" name="button" value="2">submit</button>'.
Form::close();
});
在這裏,我們得到的數據,選擇表格,這一切傳遞給驗證:
Route::post('post', function()
{
$input = Input::all();
$rules = [
'name' => 'required',
'email' => 'required'
];
$validation = Validator::make($input['form'][$input['button']], $rules);
return Redirect::back()->withInput();
});
這是你如何使用它在刀片看來,現在使用的3種形式,而不是2,當你需要,你可以有很多形式:
<h1>Create user1</h2>
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text('form[1][name]', null) }}
{{ Form::email('form[1][email]', null) }}
<button type="submit" name="button" value="1">submit</button>
{{ Form::close() }}
</h1>Create user2</h1>
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text('form[2][name]', null) }}
{{ Form::email('form[2][email]', null) }}
<button type="submit" name="button" value="2">submit</button>
{{ Form::close() }}
</h1>Create user3</h1>
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text('form[3][name]', null) }}
{{ Form::email('form[3][email]', null) }}
<button type="submit" name="button" value="3">submit</button>
{{ Form::close() }}
而且你甚至可以使用一個循環來創造刀片100種形式:
@for ($i=1; $i <= 100; $i++)
User {{$i}}
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text("form[$i][name]", null) }}
{{ Form::email("form[$i][email]", null) }}
<button type="submit" name="button" value="{{$i}}">submit</button>
{{ Form::close() }}
@endfor
您的界面在用戶體驗方面很差。如果用戶填寫了這兩個表單,但只點擊最後一個「添加」按鈕,該怎麼辦? – neoascetic
@neoascetic,他們不會像在實際應用中那樣知道形式是不同的。 – Anam
@neoascetic,在實際的應用程序中,我必須使用動態信息來創建表單(根據我從表中獲得的總體結果)。如果我有10個結果,我必須創建10個表單,並且使用單個控制器函數來處理所有表單是有意義的。 – Anam