2013-12-21 46 views
3

最酷的Laravel功能之一是,如果發生驗證錯誤,Laravel會預填充表單字段。但是,如果頁面包含more than one form,並且表單域有same name,則Laravel預填充全部forms fields如果驗證失敗,Laravel預填充多個表單

例如:

我有一個頁面,我有兩種形式,以創建新的用戶或什麼的。

<h1>Create user1</h2>  
    {{ Form::open(array('url' => 'foo/bar')) }} 
     {{ Form::text('name', null) }} 
     {{ Form::email('email', null) }} 
    {{ Form::close() }} 

</h1>Create user2</h1>  
    {{ Form::open(array('url' => 'foo/bar')) }} 
     {{ Form::text('name', null) }} 
     {{ Form::email('email', null) }} 
    {{ Form::close() }} 

控制器

class UsersController extends BaseController 
{ 
    public function store() 
    { 
    $rules = [ 
     'name' => 'required', 
     'email' => 'required' 
    ]; 

    $validation = Validator::make(Input::all(), $rules); 

    if ($validation->fails()) { 

     return Redirect::back()->withInput()->withErrors($validation); 
    } 
    } 
} 

Fill up form1 - no email

,因爲我沒有填補了電子郵件,Laravel將拋出驗證錯誤和預先填寫的表格如下:

pre-filled two forms fields

如何告訴Laravel不填滿第二個表單?

+0

您的界面在用戶體驗方面很差。如果用戶填寫了這兩個表單,但只點擊最後一個「添加」按鈕,該怎麼辦? – neoascetic

+0

@neoascetic,他們不會像在實際應用中那樣知道形式是不同的。 – Anam

+0

@neoascetic,在實際的應用程序中,我必須使用動態信息來創建表單(根據我從表中獲得的總體結果)。如果我有10個結果,我必須創建10個表單,並且使用單個控制器函數來處理所有表單是有意義的。 – Anam

回答

6

沒有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 
+0

爲什麼這個'Route :: get'? – neoascetic

+0

只需編輯以澄清Route :: get()和Route :: post()。 –

+0

@AntonioCarlosRibeiro,謝謝你的回答。我的頁面可能有2個表單,或者10個表單依賴於數據庫。現在,是否有可能使用相同的邏輯。 – Anam

相關問題