簡體   English   中英

Laravel:重復字段(和字段組):表單模型綁定

[英]Laravel: Repeating Fields (and Field Groups): Form Model Binding

我正在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

因此,用戶可以即時創建新的要素輸入,並在保存時最終組合成一個數組。 當表單中存在驗證問題並且我們被重定向回時,問題就會變成。 我還沒有找到一種方法來訪問特征數組,使其處於原有狀態(無論是否動態),以吐出它們以前的內容。 在寫這篇文章時,我想如果輸入本身引起了驗證問題,那么該問題也將清除該字段。

我已經在文檔和“ ole 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將它們動態添加到表單時,我仍然可以使用那些值。

我前段時間也遇到過類似的問題……我的代碼肯定不是很好,但它確實有效; 它可以幫助您建立一些東西...

我的竅門是為商品生成一個不同的名稱,區分舊商品和新商品,計算新商品:

<input type="text" name="E1-features"> // existing feature #1
<input type="text" name="N1-features"> // new feature #1
<input type="text" name="N3-features"> // new feature #3 (assuming user deleted #2)
<input type="hidden" name="counter" value="3"> // 3 features were added

在服務器端,控制器將現有輸入與新輸入區分開。 這是新輸入的代碼:

Input::flash();

// Existing features
foreach($features as $key => $feature){
    if (Input::get('E'.$key.'-features')){
        $rules['E'.$key.'-features'] = 'required';
    }
}

// New features
for ($i = 1;  $i <= Input::get('counter'); $i++) {
    if (Input::get('N'.$i.'-features')){
        $rules['N'.$i.'-features'] = 'required';
    }
}

$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()){
    return Redirect::to('page')->withErrors($validator)->withInput();
}else{
   // data stuff
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM