Prechádzať zdrojové kódy

默认值编辑页面显示

jqh 4 rokov pred
rodič
commit
db83bb6df4
1 zmenil súbory, kde vykonal 12 pridanie a 2 odobranie
  1. 12 2
      src/Form/Field.php

+ 12 - 2
src/Form/Field.php

@@ -62,6 +62,11 @@ class Field implements Renderable
      */
     protected $default;
 
+    /**
+     * @var bool
+     */
+    protected $allowDefaultValueInEditPage = false;
+
     /**
      * Element label.
      *
@@ -620,22 +625,26 @@ class Field implements Renderable
     /**
      * Get or set default value for field.
      *
-     * @param $default
+     * @param mixed $default
+     * @param bool  $edit
      *
      * @return $this|mixed
      */
-    public function default($default = null)
+    public function default($default = null, bool $edit = false)
     {
         if ($default === null) {
             if (
                 $this->form
                 && method_exists($this->form, 'isCreating')
                 && ! $this->form->isCreating()
+                && ! $this->allowDefaultValueInEditPage
             ) {
                 return;
             }
 
             if ($this->default instanceof \Closure) {
+                $this->default->bindTo($this->data());
+
                 return call_user_func($this->default, $this->form);
             }
 
@@ -643,6 +652,7 @@ class Field implements Renderable
         }
 
         $this->default = $default;
+        $this->allowDefaultValueInEditPage = $edit;
 
         return $this;
     }