jqh 4 years ago
parent
commit
85c961886d
2 changed files with 18 additions and 7 deletions
  1. 15 4
      src/Form/EmbeddedForm.php
  2. 3 3
      src/Form/Field/Embeds.php

+ 15 - 4
src/Form/EmbeddedForm.php

@@ -3,6 +3,7 @@
 namespace Dcat\Admin\Form;
 
 use Dcat\Admin\Form;
+use Dcat\Admin\Support\Helper;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Collection;
 
@@ -236,14 +237,14 @@ class EmbeddedForm
 
         if (is_array($jsonKey)) {
             foreach ($jsonKey as $index => $name) {
-                $elementName[$index] = "{$this->column}[$name]";
+                $elementName[$index] = $this->formatName("{$this->column}").$name;
                 $errorKey[$index] = "{$this->column}.$name";
-                $elementClass[$index] = "{$this->column}_$name";
+                $elementClass[$index] = $this->formatClass("{$this->column}_$name");
             }
         } else {
-            $elementName = "{$this->column}[$jsonKey]";
+            $elementName = $this->formatName("{$this->column}.$jsonKey");
             $errorKey = "{$this->column}.$jsonKey";
-            $elementClass = "{$this->column}_$jsonKey";
+            $elementClass = $this->formatClass("{$this->column}_$jsonKey");
         }
 
         $field->setElementName($elementName)
@@ -253,6 +254,16 @@ class EmbeddedForm
         return $field;
     }
 
+    protected function formatName($name)
+    {
+        return Helper::formatElementName($name);
+    }
+
+    protected function formatClass(string $column)
+    {
+        return str_replace('.', '-', $column);
+    }
+
     /**
      * Add a field to form.
      *

+ 3 - 3
src/Form/Field/Embeds.php

@@ -55,11 +55,11 @@ class Embeds 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);
+        //$input = Arr::only($input, $this->column);
 
         $rules = $attributes = $messages = [];
 
@@ -204,7 +204,7 @@ class Embeds extends Field
     {
         $column = array_flip($column);
 
-        foreach ($input[$this->column] as $key => $value) {
+        foreach (Arr::get($input, $this->column) as $key => $value) {
             if (! array_key_exists($key, $column)) {
                 continue;
             }