Переглянути джерело

表单上传以及多对多功能调整

jqh 5 роки тому
батько
коміт
c5680de2ee

+ 0 - 2
resources/views/form/container.blade.php

@@ -15,9 +15,7 @@
                 @endforeach
             @else
                 @foreach($form->fields() as $field)
-                    @if(! $field->hasAttribute('block-field'))
                     {!! $field->render() !!}
-                    @endif
                 @endforeach
             @endif
         </div>

+ 1 - 1
src/Form/BlockForm.php

@@ -52,7 +52,7 @@ class BlockForm extends WidgetForm
         $this->form->builder()->fields()->push($field);
         $this->fields->push($field);
 
-        $field->attribute('block-field', true);
+        $field->attribute(Builder::BUILD_IGNORE, true);
 
         $field->setForm($this->form);
         $field->width($this->width['field'], $this->width['label']);

+ 21 - 3
src/Form/Builder.php

@@ -4,10 +4,10 @@ namespace Dcat\Admin\Form;
 
 use Closure;
 use Dcat\Admin\Admin;
+use Dcat\Admin\Contracts\UploadField;
 use Dcat\Admin\Form;
 use Dcat\Admin\Form\Field\Hidden;
 use Dcat\Admin\Form\Step\Builder as StepBuilder;
-use Dcat\Admin\Form\Step\Form as StepForm;
 use Dcat\Admin\SimpleGrid;
 use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Widgets\DialogForm;
@@ -23,10 +23,15 @@ use Illuminate\Support\Str;
 class Builder
 {
     /**
-     *  Previous url key.
+     *  上个页面URL保存的key.
      */
     const PREVIOUS_URL_KEY = '_previous_';
 
+    /**
+     * 构建时需要忽略的字段.
+     */
+    const BUILD_IGNORE = 'build-ignore';
+
     /**
      * Modes constants.
      */
@@ -660,7 +665,7 @@ class Builder
     {
         foreach ($this->fields() as $field) {
             if (
-                $field instanceof Field\File
+                $field instanceof UploadField
                 || $field instanceof Form\Field\BootstrapFile
             ) {
                 return true;
@@ -742,6 +747,18 @@ class Builder
         return '</form>';
     }
 
+    /**
+     * 移除需要忽略的字段.
+     *
+     * @return void
+     */
+    protected function removeIgnoreFields()
+    {
+        $this->fields = $this->fields()->reject(function (Field $field) {
+            return $field->hasAttribute(static::BUILD_IGNORE);
+        });
+    }
+
     /**
      * Remove reserved fields like `id` `created_at` `updated_at` in form fields.
      *
@@ -796,6 +813,7 @@ class Builder
      */
     public function render()
     {
+        $this->removeIgnoreFields();
         $this->removeReservedFields();
 
         $tabObj = $this->form->getTab();

+ 27 - 1
src/Form/Concerns/HasFiles.php

@@ -27,7 +27,7 @@ trait HasFiles
             return;
         }
 
-        $field = $this->builder->field($column) ?: $this->builder->stepField($column);
+        $field = $this->findFieldByName($column);
 
         if ($field && $field instanceof UploadFieldInterface) {
             if (($results = $this->callUploading($field, $file)) && $results instanceof Response) {
@@ -44,6 +44,32 @@ trait HasFiles
         }
     }
 
+    /**
+     * 根据字段名称查找字段.
+     *
+     * @param string|null $column
+     *
+     * @return Field|null
+     */
+    public function findFieldByName(?string $column)
+    {
+        if (mb_strpos($column, '.')) {
+            [$relation, $column] = explode('.', $column);
+
+            $relation = $this->findFieldByName($relation);
+
+            if ($relation instanceof Field\HasMany) {
+                return $relation->buildNestedForm()->fields()->first(function ($field) use ($column) {
+                    return $field->column() === $column;
+                });
+            }
+
+            return null;
+        }
+
+        return $this->builder->field($column) ?: $this->builder->stepField($column);
+    }
+
     /**
      * @param array $data
      *

+ 28 - 20
src/Form/Field/File.php

@@ -14,29 +14,19 @@ class File extends Field implements UploadFieldInterface
 {
     use WebUploader, UploadField;
 
-    /**
-     * Css.
-     *
-     * @var array
-     */
     protected static $css = [
         '@webuploader',
     ];
 
-    /**
-     * Js.
-     *
-     * @var array
-     */
     protected static $js = [
         '@webuploader',
     ];
 
     protected $containerId;
 
+    protected $relationName;
+
     /**
-     * Create a new File instance.
-     *
      * @param string $column
      * @param array  $arguments
      */
@@ -50,8 +40,6 @@ class File extends Field implements UploadFieldInterface
     }
 
     /**
-     * Default directory for file to upload.
-     *
      * @return mixed
      */
     public function defaultDirectory()
@@ -98,8 +86,6 @@ class File extends Field implements UploadFieldInterface
     }
 
     /**
-     * Prepare for saving.
-     *
      * @param string $file
      *
      * @return mixed|string
@@ -115,6 +101,30 @@ class File extends Field implements UploadFieldInterface
         return $file;
     }
 
+    /**
+     * 设置字段的关联关系(在一/多对多表单中使用).
+     *
+     * @param string|null $name
+     *
+     * @return $this
+     */
+    public function setRelation(?string $name)
+    {
+        $this->relationName = $name;
+
+        $this->options['formData']['upload_column'] = $name.'.'.$this->column();
+
+        return $this;
+    }
+
+    /**
+     * @return mixed
+     */
+    public function getRelation()
+    {
+        return $this->relationName;
+    }
+
     /**
      * Set field as disabled.
      *
@@ -156,9 +166,7 @@ class File extends Field implements UploadFieldInterface
     }
 
     /**
-     * Render file upload field.
-     *
-     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
+     * @return string
      */
     public function render()
     {
@@ -168,9 +176,9 @@ class File extends Field implements UploadFieldInterface
             $this->setupPreviewOptions();
         }
 
-        $this->setupScript();
         $this->forceOptions();
         $this->formatValue();
+        $this->setupScript();
 
         $this->addVariables([
             'fileType'    => $this->options['isImage'] ? '' : 'file',

+ 9 - 11
src/Form/Field/HasMany.php

@@ -110,7 +110,7 @@ class HasMany extends Field
 
         $input = Arr::only($input, $this->column);
 
-        $form = $this->buildNestedForm($this->column, $this->builder);
+        $form = $this->buildNestedForm();
 
         $rules = $attributes = $messages = [];
 
@@ -306,7 +306,7 @@ class HasMany extends Field
      */
     protected function prepareInputValue($input)
     {
-        $form = $this->buildNestedForm($this->column, $this->builder);
+        $form = $this->buildNestedForm();
 
         return array_values(
             $form->setOriginal($this->original, $this->getKeyName())->prepare($input)
@@ -316,19 +316,17 @@ class HasMany extends Field
     /**
      * Build a Nested form.
      *
-     * @param string   $column
-     * @param \Closure $builder
      * @param null     $key
      *
      * @return NestedForm
      */
-    protected function buildNestedForm($column, \Closure $builder, $key = null)
+    public function buildNestedForm($key = null)
     {
-        $form = new Form\NestedForm($column, $key);
+        $form = new Form\NestedForm($this->column, $key);
 
         $form->setForm($this->form);
 
-        call_user_func($builder, $form);
+        call_user_func($this->builder, $form);
 
         $form->hidden($this->getKeyName());
 
@@ -425,7 +423,7 @@ class HasMany extends Field
                     continue;
                 }
 
-                $forms[$key] = $this->buildNestedForm($this->column, $this->builder, $key)
+                $forms[$key] = $this->buildNestedForm($key)
                     ->fill($data);
             }
         } else {
@@ -433,7 +431,7 @@ class HasMany extends Field
                 foreach ($this->value as $idx => $data) {
                     $key = Arr::get($data, $this->getKeyName(), $idx);
 
-                    $forms[$key] = $this->buildNestedForm($this->column, $this->builder, $key)
+                    $forms[$key] = $this->buildNestedForm($key)
                         ->fill($data);
                 }
             }
@@ -641,7 +639,7 @@ JS;
         // specify a view to render.
         $this->view = $this->views[$this->viewMode];
 
-        [$template, $script] = $this->buildNestedForm($this->column, $this->builder)
+        [$template, $script] = $this->buildNestedForm()
             ->getTemplateHtmlAndScript();
 
         $this->setupScript($script);
@@ -669,7 +667,7 @@ JS;
         $scripts = [];
 
         /* @var Field $field */
-        foreach ($this->buildNestedForm($this->column, $this->builder)->fields() as $field) {
+        foreach ($this->buildNestedForm()->fields() as $field) {
             if (is_a($field, Hidden::class)) {
                 $hidden[] = $field->render();
             } else {

+ 0 - 1
src/Form/Field/Image.php

@@ -66,7 +66,6 @@ class Image extends File
             $this->options['accept'] = [];
         }
 
-        $this->options['fileNumLimit'] = 1;
         $this->options['accept']['mimeTypes'] = 'image/*';
         $this->options['isImage'] = true;
     }

+ 6 - 6
src/Form/Field/Table.php

@@ -47,7 +47,7 @@ class Table extends HasMany
                     continue;
                 }
 
-                $forms[$key] = $this->buildNestedForm($this->column, $this->builder, $key)->fill($data);
+                $forms[$key] = $this->buildNestedForm($key)->fill($data);
             }
         } else {
             foreach ($this->value() as $key => $data) {
@@ -55,7 +55,7 @@ class Table extends HasMany
                     $data = array_merge($data, $data['pivot']);
                 }
 
-                $forms[$key] = $this->buildNestedForm($this->column, $this->builder, $key)->fill($data);
+                $forms[$key] = $this->buildNestedForm($key)->fill($data);
             }
         }
 
@@ -64,7 +64,7 @@ class Table extends HasMany
 
     protected function prepareInputValue($input)
     {
-        $form = $this->buildNestedForm($this->column, $this->builder);
+        $form = $this->buildNestedForm();
         $prepare = $form->prepare($input);
 
         return array_values(
@@ -87,14 +87,14 @@ class Table extends HasMany
         return 'id';
     }
 
-    protected function buildNestedForm($column, \Closure $builder, $key = null)
+    public function buildNestedForm($key = null)
     {
-        $form = new NestedForm($column);
+        $form = new NestedForm($this->column);
 
         $form->setForm($this->form)
             ->setKey($key);
 
-        call_user_func($builder, $form);
+        call_user_func($this->builder, $form);
 
         $form->hidden(NestedForm::REMOVE_FLAG_NAME)->default(0)->addElementClass(NestedForm::REMOVE_FLAG_CLASS);
 

+ 4 - 6
src/Form/Field/WebUploader.php

@@ -85,7 +85,7 @@ trait WebUploader
     }
 
     /**
-     * Set upload server.
+     * 设置上传接口.
      *
      * @param string $server
      *
@@ -99,7 +99,7 @@ trait WebUploader
     }
 
     /**
-     * Disable auto save the path of uploaded file.
+     * 禁止上传文件后自动更新字段值.
      *
      * @return $this
      */
@@ -195,7 +195,7 @@ trait WebUploader
     }
 
     /**
-     * Get create url.
+     * 获取创建链接.
      *
      * @return string
      */
@@ -205,7 +205,7 @@ trait WebUploader
     }
 
     /**
-     * Set preview options form image field.
+     * 图片预览设置.
      *
      * @return void
      */
@@ -215,8 +215,6 @@ trait WebUploader
     }
 
     /**
-     * Set options for file-upload plugin.
-     *
      * @param array $options
      *
      * @return $this

+ 8 - 0
src/Form/NestedForm.php

@@ -3,6 +3,7 @@
 namespace Dcat\Admin\Form;
 
 use Dcat\Admin\Admin;
+use Dcat\Admin\Contracts\UploadField;
 use Dcat\Admin\Form;
 use Dcat\Admin\Widgets\Form as WidgetForm;
 use Illuminate\Support\Arr;
@@ -314,6 +315,13 @@ class NestedForm
     {
         $this->fields->push($field);
 
+        $this->form->builder()->fields()->push($field);
+        $field->attribute(Builder::BUILD_IGNORE, true);
+
+        if ($field instanceof UploadField) {
+            $field->setRelation($this->relationName);
+        }
+
         $field::collectAssets();
 
         return $this;

+ 2 - 2
src/Form/StepForm.php

@@ -23,7 +23,7 @@ class StepForm extends WidgetForm
     protected $form;
 
     /**
-     * @var Builder
+     * @var ParentForm\Step\Builder
      */
     protected $parent;
 
@@ -59,7 +59,7 @@ class StepForm extends WidgetForm
     }
 
     /**
-     * @param Form $form
+     * @param ParentForm $form
      *
      * @return $this
      */