瀏覽代碼

调整表单保存以及分步表单部分代码

jqh 5 年之前
父節點
當前提交
0d8215a51a
共有 3 個文件被更改,包括 155 次插入118 次删除
  1. 54 117
      src/Form.php
  2. 1 1
      src/Form/Concerns/HasFiles.php
  3. 100 0
      src/Form/Concerns/HasSteps.php

+ 54 - 117
src/Form.php

@@ -89,6 +89,7 @@ class Form implements Renderable
     use HasBuilderEvents,
         Concerns\HasEvents,
         Concerns\HasFiles,
+        Concerns\HasSteps,
         Macroable {
             __call as macroCall;
         }
@@ -572,38 +573,7 @@ class Form implements Renderable
     {
         $data = $data ?: $this->request->all();
 
-        $this->build();
-
-        $this->prepareStepFormFields($data);
-
-        // Validate step form.
-        if ($this->isStepFormValidationRequest()) {
-            return $this->validateStepForm($data);
-        }
-
-        if (($response = $this->callSubmitted())) {
-            return $response;
-        }
-
-        if ($response = $this->handleUploadFile($data)) {
-            return $response;
-        }
-        if ($response = $this->handleFileDeleteBeforeCreate($data)) {
-            return $response;
-        }
-
-        if ($response = $this->handleFileDeleteWhenCreating($data)) {
-            return $response;
-        }
-
-        // Handle validation errors.
-        if ($validationMessages = $this->validationMessages($data)) {
-            return $this->makeValidationErrorsResponse($validationMessages);
-        }
-
-        if (($response = $this->prepare($data))) {
-            $this->deleteFilesWhenCreating($data);
-
+        if ($response = $this->beforeStore($data)) {
             return $response;
         }
 
@@ -627,86 +597,49 @@ class Form implements Renderable
         );
     }
 
+
     /**
+     * Before store.
+     *
      * @param array $data
-     * @return void
+     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|Response|void
      */
-    protected function prepareStepFormFields(array $data)
+    protected function beforeStore(array $data)
     {
-        $stepBuilder = $this->builder->getStepBuilder();
-
-        if (
-            empty($stepBuilder)
-            || empty($stepBuilder->count())
-            || (! isset($data[StepBuilder::ALL_STEPS]) && ! $this->isStepFormValidationRequest())
-        ) {
-            return;
-        }
+        $this->build();
 
-        $steps = $stepBuilder->all();
+        $this->prepareStepFormFields($data);
 
+        // Validate step form.
         if ($this->isStepFormValidationRequest()) {
-            $currentIndex = $data[StepBuilder::CURRENT_VALIDATION_STEP];
-
-            if (empty($steps[$currentIndex])) {
-                return;
-            }
-
-            foreach ($steps[$currentIndex]->field() as $field) {
-                $this->pushField($field);
-            }
-            return;
+            return $this->validateStepForm($data);
         }
 
-        if (! empty($data[StepBuilder::ALL_STEPS])) {
-            foreach ($steps as $stepForm) {
-                foreach ($stepForm->field() as $field) {
-                    $this->pushField($field);
-                }
-            }
+        if (($response = $this->callSubmitted())) {
+            return $response;
         }
-    }
 
-    /**
-     * @return bool
-     */
-    protected function isStepFormValidationRequest()
-    {
-        $index = $this->request->get(StepBuilder::CURRENT_VALIDATION_STEP);
+        if ($response = $this->handleUploadFile($data)) {
+            return $response;
+        }
+        if ($response = $this->handleFileDeleteBeforeCreate($data)) {
+            return $response;
+        }
 
-        return $index !== '' && $index !== null;
-    }
+        if ($response = $this->handleFileDeleteWhenCreating($data)) {
+            return $response;
+        }
 
-    /**
-     * Validate step form.
-     *
-     * @param array $data
-     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
-     */
-    protected function validateStepForm(array $data)
-    {
         // Handle validation errors.
         if ($validationMessages = $this->validationMessages($data)) {
             return $this->makeValidationErrorsResponse($validationMessages);
         }
 
-        return $this->ajaxResponse('Success');
-    }
+        if (($response = $this->prepare($data))) {
+            $this->deleteFilesWhenCreating($data);
 
-    /**
-     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|void
-     */
-    protected function responseDoneStep()
-    {
-        if (! $builder = $this->builder->getStepBuilder()) {
-            return;
+            return $response;
         }
-
-        return response(
-            $builder->getDoneStep()
-                ->finish()
-                ->render()
-        );
     }
 
     /**
@@ -801,7 +734,7 @@ class Form implements Renderable
      * @param int   $id
      * @param array $input
      *
-     * @return bool||Response
+     * @return Response
      */
     protected function handleOrderable(array $input = [])
     {
@@ -829,12 +762,38 @@ class Form implements Renderable
     {
         $data = $data ?: $this->request->all();
 
+        if ($response = $this->beforeUpdate($id, $data)) {
+            return $response;
+        }
+
+        $this->updates = $this->prepareUpdate($this->updates);
+
+        $this->repository->update($this);
+
+        if (($result = $this->callSaved())) {
+            return $result;
+        }
+
+        return $this->redirect(
+            $this->getRedirectUrl($id, $redirectTo),
+            trans('admin.update_succeeded')
+        );
+    }
+
+    /**
+     * Before update.
+     *
+     * @param array $data
+     * @return Response|void
+     */
+    protected function beforeUpdate($id, array &$data)
+    {
         $this->builder->setResourceId($id);
         $this->builder->setMode(Builder::MODE_EDIT);
 
         $this->build();
 
-        if (($response = $this->callSubmitted())) {
+        if ($response = $this->callSubmitted()) {
             return $response;
         }
 
@@ -862,22 +821,9 @@ class Form implements Renderable
             );
         }
 
-        if (($response = $this->prepare($data))) {
+        if ($response = $this->prepare($data)) {
             return $response;
         }
-
-        $this->updates = $this->prepareUpdate($this->updates);
-
-        $this->repository->update($this);
-
-        if (($result = $this->callSaved())) {
-            return $result;
-        }
-
-        return $this->redirect(
-            $this->getRedirectUrl($id, $redirectTo),
-            trans('admin.update_succeeded')
-        );
     }
 
     /**
@@ -1466,15 +1412,6 @@ class Form implements Renderable
         return $this->rows;
     }
 
-    /**
-     * @param Closure|null $builder
-     * @return StepBuilder
-     */
-    public function step(\Closure $builder = null)
-    {
-        return $this->builder->step($builder);
-    }
-
     /**
      * Tools setting for form.
      *

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

@@ -95,7 +95,7 @@ trait HasFiles
             unset($input['key']);
         }
 
-        request()->replace($input);
+        $this->request->replace($input);
 
         return $input;
     }

+ 100 - 0
src/Form/Concerns/HasSteps.php

@@ -0,0 +1,100 @@
+<?php
+
+namespace Dcat\Admin\Form\Concerns;
+
+use Closure;
+use Dcat\Admin\Form\StepBuilder;
+
+trait HasSteps
+{
+    /**
+     * @param Closure|null $builder
+     * @return StepBuilder
+     */
+    public function step(\Closure $builder = null)
+    {
+        return $this->builder->step($builder);
+    }
+
+    /**
+     * @param array $data
+     * @return void
+     */
+    protected function prepareStepFormFields(array $data)
+    {
+        $stepBuilder = $this->builder->getStepBuilder();
+
+        if (
+            empty($stepBuilder)
+            || empty($stepBuilder->count())
+            || (! isset($data[StepBuilder::ALL_STEPS]) && ! $this->isStepFormValidationRequest())
+        ) {
+            return;
+        }
+
+        $steps = $stepBuilder->all();
+
+        if ($this->isStepFormValidationRequest()) {
+            $currentIndex = $data[StepBuilder::CURRENT_VALIDATION_STEP];
+
+            if (empty($steps[$currentIndex])) {
+                return;
+            }
+
+            foreach ($steps[$currentIndex]->field() as $field) {
+                $this->pushField($field);
+            }
+            return;
+        }
+
+        if (! empty($data[StepBuilder::ALL_STEPS])) {
+            foreach ($steps as $stepForm) {
+                foreach ($stepForm->field() as $field) {
+                    $this->pushField($field);
+                }
+            }
+        }
+    }
+
+    /**
+     * @return bool
+     */
+    protected function isStepFormValidationRequest()
+    {
+        $index = $this->request->get(StepBuilder::CURRENT_VALIDATION_STEP);
+
+        return $index !== '' && $index !== null;
+    }
+
+    /**
+     * Validate step form.
+     *
+     * @param array $data
+     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
+     */
+    protected function validateStepForm(array $data)
+    {
+        // Handle validation errors.
+        if ($validationMessages = $this->validationMessages($data)) {
+            return $this->makeValidationErrorsResponse($validationMessages);
+        }
+
+        return $this->ajaxResponse('Success');
+    }
+
+    /**
+     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|void
+     */
+    protected function responseDoneStep()
+    {
+        if (! $builder = $this->builder->getStepBuilder()) {
+            return;
+        }
+
+        return response(
+            $builder->getDoneStep()
+                ->finish()
+                ->render()
+        );
+    }
+}