我正在Laravel中構建一個表單,處理可重複實體(無論是單個輸入還是字段組)形式的數組字段。當遇到重複字段的驗證錯誤或表單中的不同輸入時,我遇到了使用表單模型綁定的問題。Laravel:重複字段(和字段組):表單模型綁定
現在,我通過在視圖中拉動局部使用AJAX
# Add Feature
$(document).on 'click', '.js-listing__add-feature', (e) ->
e.preventDefault()
$.ajax
url: dashboard.partials.feature
type: 'GET'
success: (data) ->
$data = $(data)
$('.js-listing__features').append $data
return
return
# Removing features
$(document).on 'click', '.js-listing__remove-feature', (e) ->
e.preventDefault()
$(this).parent('.js-listing__feature-wrapper').remove()
return
因此產生新的每個字段的「實例」,用戶可以在其最終組合成一個陣列動態創建新特徵輸入保存時。當表單中存在驗證問題並且我們被重定向時,問題就變成了。我還沒有找到一種方法來訪問它所處的狀態(動態或非動態)功能陣列吐出他們以前有什麼。在寫這篇文章時,我想這個問題也會變成清除這個領域,如果它是導致驗證問題的輸入。
我在文檔和'谷歌Google搜索這個主題的靈感,但沒有遇到任何東西。任何向正確的方向推動都會非常有幫助。一如既往地感謝!
形式
@extends('dashboard.master')
@section('content')
<h1>Edit Listing</h1>
@include('dashboard.partials.errors')
{!! Form::model($listing, ['method' => 'PATCH', 'route' => ['dashboard.listings.update', $listing->id], 'class' => 'uk-form']) !!}
<div class="uk-form-row">
{!! Form::label('price', 'Price') !!}
{!! Form::text('price') !!}
</div>
<div class="uk-form-row js-listing__features">
{!! Form::label('features', 'Features') !!}
@if ($listing->features && count($listing->features))
@foreach($listing->features as $key => $feature)
<div class="js-listing__feature-wrapper">
<input type="text" name="features[]" value="{{$feature}}">
<a class="js-listing__add-feature" href="#">+</a>
@if ($key > 0)
<a class="js-listing__remove-feature" href="#">-</a>
@endif
</div>
@endforeach
@else
<div class="js-listing__feature-wrapper">
<input type="text" name="features[]">
<a class="js-listing__add-feature" href="#">+</a>
</div>
@endif
</div>
<div class="uk-form-row">
{!! Form::submit('Update Listing') !!}
</div>
{!! Form::close() !!}
@stop
你會看到我採取什麼我爲@foreach
做時,我有價值觀編輯上市時顯示他們的例子。這裏的問題是沒有讀取值(我已設置/獲取屬性工作正常),但如何表單模型綁定與輸入數組工作,所以我仍然可以使用這些值時,他們已被動態地添加到窗體與AJAX 。
請問您可以舉一個例子,說明您的表單看起來是什麼樣子,以及您在哪裏面臨問題? – Dhiraj 2015-04-04 16:39:09
當然@DhirajBodicherla我已經在我的原始問題中添加了表單示例。謝謝! – Zach 2015-04-04 21:22:38
如果你可以在小提琴或其他地方展示它,那麼解決你的問題將會更容易。 – 2015-04-09 15:21:32