Quellcode durchsuchen

修复Form::list字段无法清空所有数据,以及无法显示验证错误信息问题

jqh vor 5 Jahren
Ursprung
Commit
3127d867ef
2 geänderte Dateien mit 38 neuen und 9 gelöschten Zeilen
  1. 7 2
      resources/views/form/listfield.blade.php
  2. 31 7
      src/Form/Field/ListField.php

+ 7 - 2
resources/views/form/listfield.blade.php

@@ -11,6 +11,10 @@
                 <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$message}}</label><br/>
             @endforeach
         @endif
+        <error></error>
+
+        <span name="{{$name}}"></span>
+        <input name="{{ $name }}[values][{{ \Dcat\Admin\Form\Field\ListField::DEFAULT_FLAG_NAME }}]" type="hidden" />
 
         <table class="table table-hover">
 
@@ -24,7 +28,8 @@
                     <td>
                         <div class="form-group {{ $errors->has($itemErrorKey) ? 'has-error' : '' }}">
                             <div class="col-sm-12">
-                                <input name="{{ $column }}[values][]" value="{{ old("{$column}.values.{$k}", $v) }}" class="form-control" />
+                                <input name="{{ $name }}[values][{{ $k }}]" value="{{ old("{$column}.values.{$k}", $v) }}" class="form-control" />
+                                <error></error>
                                 @if($errors->has($itemErrorKey))
                                     @foreach($errors->get($itemErrorKey) as $message)
                                         <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$message}}</label><br/>
@@ -61,7 +66,7 @@
         <td>
             <div class="form-group">
                 <div class="col-sm-12">
-                    <input name="{{ $column }}[values][]" class="form-control" />
+                    <input name="{{ $name }}[values][{key}]" class="form-control" />
                 </div>
             </div>
         </td>

+ 31 - 7
src/Form/Field/ListField.php

@@ -8,6 +8,8 @@ use Illuminate\Support\Arr;
 
 class ListField extends Field
 {
+    const DEFAULT_FLAG_NAME = '_def_';
+
     /**
      * Max list size.
      *
@@ -103,22 +105,38 @@ class ListField extends Field
 
         $attributes["{$this->column}.values"] = $this->label;
 
+        $input = $this->prepareValidatorInput($input);
+
         return validator($input, $rules, $this->getValidationMessages(), $attributes);
     }
 
+    protected function prepareValidatorInput(array $input)
+    {
+        Arr::forget($input, "{$this->column}.values.".static::DEFAULT_FLAG_NAME);
+
+        return $input;
+    }
+
     /**
      * {@inheritdoc}
      */
     protected function setupScript()
     {
+        $number = $this->value ? count($this->value) : 0;
+
         $this->script = <<<JS
-$('.{$this->column}-add').on('click', function () {
-    var tpl = $('template.{$this->column}-tpl').html();
-    $('tbody.list-{$this->column}-table').append(tpl);
-});
-$('tbody').on('click', '.{$this->column}-remove', function () {
-    $(this).closest('tr').remove();
-});
+(function () {
+    var index = {$number};
+    $('.{$this->column}-add').on('click', function () {
+        var tpl = $('template.{$this->column}-tpl').html().replace('{key}', index);
+        $('tbody.list-{$this->column}-table').append(tpl);
+        
+        index++;
+    });
+    $('tbody').on('click', '.{$this->column}-remove', function () {
+        $(this).closest('tr').remove();
+    });
+})();
 JS;
     }
 
@@ -127,6 +145,12 @@ JS;
      */
     public function prepare($value)
     {
+        unset($value['values'][static::DEFAULT_FLAG_NAME]);
+
+        if (empty($value['values'])) {
+            return [];
+        }
+
         return array_values($value['values']);
     }