Browse Source

修复当hasMany表单与父表有同样字段名称时填充值默认值异常问题

jqh 4 years ago
parent
commit
df59458a3d
5 changed files with 11 additions and 11 deletions
  1. 1 1
      src/Form/BlockForm.php
  2. 1 6
      src/Form/Builder.php
  3. 2 0
      src/Form/Field.php
  4. 4 3
      src/Form/NestedForm.php
  5. 3 1
      src/Widgets/Form.php

+ 1 - 1
src/Form/BlockForm.php

@@ -103,7 +103,7 @@ class BlockForm extends WidgetForm
             $this->layout()->addField($field);
         }
 
-        $field->attribute(Builder::BUILD_IGNORE, true);
+        $field->attribute(Field::BUILD_IGNORE, true);
 
         $field->setForm($this->form);
         $field->width($this->width['field'], $this->width['label']);

+ 1 - 6
src/Form/Builder.php

@@ -28,11 +28,6 @@ class Builder
      */
     const PREVIOUS_URL_KEY = '_previous_';
 
-    /**
-     * 构建时需要忽略的字段.
-     */
-    const BUILD_IGNORE = 'build-ignore';
-
     /**
      * Modes constants.
      */
@@ -672,7 +667,7 @@ class Builder
     protected function removeIgnoreFields()
     {
         $this->fields = $this->fields()->reject(function (Field $field) {
-            return $field->hasAttribute(static::BUILD_IGNORE);
+            return $field->hasAttribute(Field::BUILD_IGNORE);
         });
     }
 

+ 2 - 0
src/Form/Field.php

@@ -28,6 +28,8 @@ class Field implements Renderable
 
     const FIELD_CLASS_PREFIX = 'field_';
 
+    const BUILD_IGNORE = 'build-ignore';
+
     /**
      * Element value.
      *

+ 4 - 3
src/Form/NestedForm.php

@@ -57,6 +57,8 @@ class NestedForm extends WidgetForm
      */
     public function __construct($relation = null, $key = null)
     {
+        parent::__construct();
+
         $this->relationName = $relation;
 
         $this->key = $key;
@@ -65,8 +67,6 @@ class NestedForm extends WidgetForm
         $this->disableSubmitButton();
         $this->ajax(false);
         $this->useFormTag(false);
-
-        parent::__construct();
     }
 
     /**
@@ -283,7 +283,8 @@ class NestedForm extends WidgetForm
 
         if (method_exists($this->form, 'builder')) {
             $this->form->builder()->fields()->push($field);
-            $field->attribute(Builder::BUILD_IGNORE, true);
+            $this->form->ignore($field->column());
+            $field->attribute(Field::BUILD_IGNORE, true);
         }
 
         $field->setNestedFormRelation([

+ 3 - 1
src/Widgets/Form.php

@@ -628,7 +628,9 @@ HTML;
     public function fillFields(array $data)
     {
         foreach ($this->fields as $field) {
-            $field->fill($data);
+            if (! $field->hasAttribute(Field::BUILD_IGNORE)) {
+                $field->fill($data);
+            }
         }
     }