Jelajahi Sumber

简化表单新增页面文件删除功能代码

jqh 4 tahun lalu
induk
melakukan
15d1819adc
3 mengubah file dengan 19 tambahan dan 73 penghapusan
  1. 2 2
      src/Form/BlockForm.php
  2. 15 49
      src/Form/Concerns/HasFiles.php
  3. 2 22
      src/Widgets/Form.php

+ 2 - 2
src/Form/BlockForm.php

@@ -66,8 +66,8 @@ class BlockForm extends WidgetForm
     public function showFooter()
     {
         $this->ajax(true);
-        $this->disableSubmitButton(false);
-        $this->disableResetButton(false);
+        $this->submitButton(true);
+        $this->resetButton(true);
 
         return $this;
     }

+ 15 - 49
src/Form/Concerns/HasFiles.php

@@ -79,23 +79,26 @@ trait HasFiles
      */
     protected function deleteFileWhenCreating(array $input)
     {
-        if ($response = $this->deleteFileIfIsFileDeleteRequest($input)) {
-            return $response;
+        if (! array_key_exists(Field::FILE_DELETE_FLAG, $input)) {
+            return;
         }
 
-        $input = $this->handleFileDelete($input);
-
         $column = $input['_column'] ?? null;
+        $filePath = $input['key'] ?? null;
+        $relation = $input['_relation'] ?? null;
 
-        if (isset($input[Field::FILE_DELETE_FLAG]) && $column) {
-            $this->builder->fields()->filter(function ($field) use ($column) {
-                /* @var Field $field */
+        if (! $column && ! $filePath) {
+            return;
+        }
 
-                return $column === $field->column() && $field instanceof UploadFieldInterface;
-            })->each(function (UploadFieldInterface $file) use ($input) {
-                /* @var Field $file */
-                $this->deleteFile($file, $input[Field::FILE_DELETE_FLAG]);
-            });
+        if (empty($relation)) {
+            $field = $this->findFieldByName($column);
+        } else {
+            $field = $this->getFieldByRelationName($relation[0], $column);
+        }
+
+        if ($field && $field instanceof UploadFieldInterface) {
+            $this->deleteFile($field, $filePath);
 
             return $this
                 ->response()
@@ -132,43 +135,6 @@ trait HasFiles
         $this->callFileDeleted($field);
     }
 
-    /**
-     * 如果是删除文件请求,则直接删除文件.
-     *
-     * @param array $data
-     *
-     * @return \Illuminate\Http\JsonResponse|void
-     */
-    protected function deleteFileIfIsFileDeleteRequest(array $data)
-    {
-        if (! array_key_exists(Field::FILE_DELETE_FLAG, $data)) {
-            return;
-        }
-
-        $column = $data['_column'] ?? null;
-        $filePath = $data['key'] ?? null;
-        $relation = $data['_relation'] ?? null;
-
-        if (! $column && ! $filePath) {
-            return;
-        }
-
-        if (empty($relation)) {
-            $field = $this->findFieldByName($column);
-        } else {
-            $field = $this->getFieldByRelationName($relation[0], $column);
-        }
-
-        if ($field && $field instanceof UploadFieldInterface) {
-            $this->deleteFile($field, $filePath);
-
-            return $this
-                ->response()
-                ->status(true)
-                ->send();
-        }
-    }
-
     /**
      * 获取hasMany的子表单字段.
      *

+ 2 - 22
src/Widgets/Form.php

@@ -443,18 +443,6 @@ class Form implements Renderable
         return $messageBag;
     }
 
-    /**
-     * Disable Pjax.
-     *
-     * @return $this
-     */
-    public function disablePjax()
-    {
-        $this->forgetHtmlAttribute('pjax-container');
-
-        return $this;
-    }
-
     public function useFormTag(bool $tag = true)
     {
         $this->useFormTag = $tag;
@@ -492,14 +480,10 @@ class Form implements Renderable
      * @param bool $value
      *
      * @return $this
-     *
-     * @deprecated 即将废弃,请使用 resetButton 代替
      */
     public function disableResetButton(bool $value = true)
     {
-        $this->buttons['reset'] = ! $value;
-
-        return $this;
+        return $this->resetButton(! $value);
     }
 
     /**
@@ -508,14 +492,10 @@ class Form implements Renderable
      * @param bool $value
      *
      * @return $this
-     *
-     * @deprecated 即将废弃,请使用 submitButton 代替
      */
     public function disableSubmitButton(bool $value = true)
     {
-        $this->buttons['submit'] = ! $value;
-
-        return $this;
+        return $this->submitButton(! $value);
     }
 
     /**