소스 검색

Form::table以及Form::array表单支持关联关系字段

jqh 4 년 전
부모
커밋
3b9bbe5539
4개의 변경된 파일62개의 추가작업 그리고 40개의 파일을 삭제
  1. 1 25
      src/Form/Field.php
  2. 10 10
      src/Form/Field/HasMany.php
  3. 15 4
      src/Form/NestedForm.php
  4. 36 1
      src/Support/Helper.php

+ 1 - 25
src/Form/Field.php

@@ -320,31 +320,7 @@ class Field implements Renderable
      */
     protected function formatName($column)
     {
-        if (is_string($column)) {
-            $name = explode('.', $column);
-
-            if (count($name) == 1) {
-                return $name[0];
-            }
-
-            $html = array_shift($name);
-            foreach ($name as $piece) {
-                $html .= "[$piece]";
-            }
-
-            return $html;
-        }
-
-        if (is_array($this->column)) {
-            $names = [];
-            foreach ($this->column as $key => $name) {
-                $names[$key] = $this->formatName($name);
-            }
-
-            return $names;
-        }
-
-        return '';
+        return Helper::formatElementName($column);
     }
 
     /**

+ 10 - 10
src/Form/Field/HasMany.php

@@ -113,12 +113,10 @@ class HasMany extends Field
      */
     public function getValidator(array $input)
     {
-        if (! array_key_exists($this->column, $input)) {
+        if (! Arr::has($input, $this->column)) {
             return false;
         }
 
-        $input = Arr::only($input, $this->column);
-
         $form = $this->buildNestedForm();
 
         $rules = $attributes = $messages = [];
@@ -168,15 +166,17 @@ class HasMany extends Field
         $newInput = [];
 
         foreach ($rules as $column => $rule) {
-            foreach (array_keys($input[$this->column]) as $key) {
-                if ($input[$this->column][$key][NestedForm::REMOVE_FLAG_NAME]) {
+            foreach (array_keys(Arr::get($input, $this->column)) as $key) {
+                if (Arr::get($input, "{$this->column}.{$key}.".NestedForm::REMOVE_FLAG_NAME)) {
                     continue;
                 }
 
                 $newRules["{$this->column}.$key.$column"] = $rule;
-                if (isset($input[$this->column][$key][$column]) &&
-                    is_array($input[$this->column][$key][$column])) {
-                    foreach ($input[$this->column][$key][$column] as $vkey => $value) {
+
+                $ruleValue = Arr::get($input, "{$this->column}.$key.$column");
+
+                if (is_array($ruleValue)) {
+                    foreach ($ruleValue as $vkey => $value) {
                         $newInput["{$this->column}.$key.{$column}$vkey"] = $value;
                     }
                 }
@@ -201,7 +201,7 @@ class HasMany extends Field
     protected function formatValidationMessages(array $input, array $messages)
     {
         $result = [];
-        foreach ($input[$this->column] as $key => &$value) {
+        foreach (Arr::get($input, $this->column) as $key => &$value) {
             $newKey = $this->column.'.'.$key;
 
             foreach ($messages as $k => $message) {
@@ -278,7 +278,7 @@ class HasMany extends Field
          *
          * in the HasMany relation, has many data/field set, $set is field set in the below
          */
-        foreach ($input[$this->column] as $index => $set) {
+        foreach (Arr::get($input, $this->column) as $index => $set) {
 
             /*
              * foreach the field set to find the corresponding $column

+ 15 - 4
src/Form/NestedForm.php

@@ -6,6 +6,7 @@ use Dcat\Admin\Admin;
 use Dcat\Admin\Form;
 use Dcat\Admin\Form\Field\MultipleSelectTable;
 use Dcat\Admin\Form\Field\SelectTable;
+use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Widgets\Form as WidgetForm;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Collection;
@@ -413,13 +414,13 @@ class NestedForm
         if (is_array($column)) {
             foreach ($column as $k => $name) {
                 $errorKey[$k] = sprintf('%s.%s.%s', $this->relationName, $key, $name);
-                $elementName[$k] = sprintf('%s[%s][%s]', $this->relationName, $key, $name);
-                $elementClass[$k] = [$this->relationName, $name];
+                $elementName[$k] = sprintf('%s[%s][%s]', $this->formatName(), $key, $name);
+                $elementClass[$k] = [$this->formatClass(), $this->formatClass($name)];
             }
         } else {
             $errorKey = sprintf('%s.%s.%s', $this->relationName, $key, $column);
-            $elementName = sprintf('%s[%s][%s]', $this->relationName, $key, $column);
-            $elementClass = [$this->relationName, $column];
+            $elementName = sprintf('%s[%s][%s]', $this->formatName(), $key, $column);
+            $elementClass = [$this->formatClass(), $this->formatClass($column)];
         }
 
         return $field->setErrorKey($errorKey)
@@ -427,6 +428,16 @@ class NestedForm
             ->setElementClass($elementClass);
     }
 
+    protected function formatClass($name = null)
+    {
+        return str_replace('.', '_', $name ?: $this->relationName);
+    }
+
+    protected function formatName($name = null)
+    {
+        return Helper::formatElementName($name ?: $this->relationName);
+    }
+
     /**
      * Add nested-form fields dynamically.
      *

+ 36 - 1
src/Support/Helper.php

@@ -767,7 +767,7 @@ class Helper
         foreach ($relations as $first => $v) {
             if (isset($input[$first])) {
                 foreach ($input[$first] as $key => $value) {
-                    if (is_array($value) && ! Arr::isAssoc($value)) {
+                    if (is_array($value)) {
                         $input["$first.$key"] = $value;
                     }
                 }
@@ -846,4 +846,39 @@ class Helper
 
         return $item;
     }
+
+    /**
+     * 格式化表单元素 name 属性.
+     *
+     * @param string|array $name
+     *
+     * @return mixed|string
+     */
+    public static function formatElementName($name)
+    {
+        if (! $name) {
+            return $name;
+        }
+
+        if (is_array($name)) {
+            foreach ($name as &$v) {
+                $v = static::formatElementName($name);
+            }
+
+            return $name;
+        }
+
+        $name = explode('.', $name);
+
+        if (count($name) == 1) {
+            return $name[0];
+        }
+
+        $html = array_shift($name);
+        foreach ($name as $piece) {
+            $html .= "[$piece]";
+        }
+
+        return $html;
+    }
 }