jqh 5 năm trước cách đây
mục cha
commit
66335da746

+ 5 - 0
src/Form/Field.php

@@ -602,6 +602,11 @@ class Field implements Renderable
         return $this;
     }
 
+    public function old()
+    {
+        return old($this->column, $this->value());
+    }
+
     /**
      * Get original value of the field.
      *

+ 2 - 2
src/Form/Field/BootstrapFile.php

@@ -142,7 +142,7 @@ class BootstrapFile extends Field
      */
     protected function preview()
     {
-        return $this->objectUrl($this->value);
+        return $this->objectUrl($this->value());
     }
 
     /**
@@ -163,7 +163,7 @@ class BootstrapFile extends Field
     protected function initialPreviewConfig()
     {
         return [
-            ['caption' => basename($this->value), 'key' => 0],
+            ['caption' => basename($this->value()), 'key' => 0],
         ];
     }
 

+ 1 - 1
src/Form/Field/BootstrapMultipleFile.php

@@ -219,7 +219,7 @@ class BootstrapMultipleFile extends Field
 
         $this->setupDefaultOptions();
 
-        if (!empty($this->value)) {
+        if (!empty($this->value())) {
             $this->options(['initialPreview' => $this->preview()]);
             $this->setupPreviewOptions();
         }

+ 1 - 1
src/Form/Field/Checkbox.php

@@ -118,7 +118,7 @@ class Checkbox extends MultipleSelect
     {
         if ($this->options instanceof \Closure) {
             $this->options(
-                $this->options->call($this->getFormModel(), $this->value, $this)
+                $this->options->call($this->getFormModel(), $this->value(), $this)
             );
         }
 

+ 1 - 1
src/Form/Field/Display.php

@@ -17,7 +17,7 @@ class Display extends Field
     public function render()
     {
         if ($this->callback instanceof Closure) {
-            $this->value = $this->callback->call($this->getFormModel(), $this->value);
+            $this->value = $this->callback->call($this->getFormModel(), $this->value());
         }
 
         return parent::render();

+ 4 - 2
src/Form/Field/File.php

@@ -135,7 +135,7 @@ class File extends Field
     {
         $previews = [];
 
-        foreach ($this->value as $value) {
+        foreach ($this->value() as $value) {
             $previews[] = [
                 'id' => $value,
                 'path' => basename($value),
@@ -160,7 +160,7 @@ class File extends Field
     {
         $this->setDefaultServer();
 
-        if (!empty($this->value)) {
+        if (!empty($this->value())) {
             $this->setupPreviewOptions();
         }
 
@@ -168,6 +168,8 @@ class File extends Field
 
         if ($this->value !== null) {
             $this->value = join(',', $this->value);
+        } elseif (is_array($this->default)) {
+            $this->default = join(',', $this->default);
         }
         $this->addVariables([
             'options' => json_encode($this->options),

+ 1 - 1
src/Form/Field/KeyValue.php

@@ -71,7 +71,7 @@ class KeyValue extends Field
 
     protected function setupScript()
     {
-        $value = old($this->column, $this->value);
+        $value = old($this->column, $this->value());
 
         $number = $value ? count($value) : 0;
 

+ 1 - 1
src/Form/Field/ListField.php

@@ -122,7 +122,7 @@ class ListField extends Field
      */
     protected function setupScript()
     {
-        $value = old($this->column, $this->value);
+        $value = old($this->column, $this->value());
 
         $number = $value ? count($value) : 0;
 

+ 1 - 1
src/Form/Field/Radio.php

@@ -88,7 +88,7 @@ class Radio extends Field
     {
         if ($this->options instanceof \Closure) {
             $this->options(
-                $this->options->call($this->getFormModel(), $this->value, $this)
+                $this->options->call($this->getFormModel(), $this->value(), $this)
             );
         }
 

+ 2 - 2
src/Form/Field/Select.php

@@ -374,7 +374,7 @@ JS;
         if ($this->options instanceof \Closure) {
             $this->options = $this->options->bindTo($this->getFormModel());
 
-            $this->options(call_user_func($this->options, $this->value, $this));
+            $this->options(call_user_func($this->options, $this->value(), $this));
         }
 
         $this->options = array_filter($this->options, 'strlen');
@@ -384,7 +384,7 @@ JS;
             'groups'  => $this->groups,
         ]);
 
-        $this->attribute('data-value', implode(',', (array) $this->value()));
+        $this->attribute('data-value', implode(',', Helper::array($this->value())));
 
         return parent::render();
     }

+ 1 - 1
src/Form/Field/SelectResource.php

@@ -72,7 +72,7 @@ class SelectResource extends Field
     protected function formatOptions()
     {
         if ($this->options instanceof \Closure) {
-            $value = Helper::array(old($this->column, $this->value));
+            $value = Helper::array(old($this->column, $this->value()));
 
             $this->options = $this->options->call($this->getFormModel(), $value, $this);
         }

+ 1 - 1
src/Form/Field/Table.php

@@ -48,7 +48,7 @@ class Table extends HasMany
                 $forms[$key] = $this->buildNestedForm($this->column, $this->builder, $key)->fill($data);
             }
         } else {
-            foreach ($this->value as $key => $data) {
+            foreach ($this->value() as $key => $data) {
                 if (isset($data['pivot'])) {
                     $data = array_merge($data, $data['pivot']);
                 }

+ 7 - 5
src/Form/Field/Tags.php

@@ -144,10 +144,10 @@ class Tags extends Field
     public function value($value = null)
     {
         if (is_null($value)) {
-            return empty($this->value) ? ($this->getDefault() ?? []) : $this->value;
+            return empty($this->value) ? Helper::array($this->getDefault()) : $this->value;
         }
 
-        $this->value = (array) $value;
+        $this->value = Helper::array($value);
 
         return $this;
     }
@@ -157,9 +157,11 @@ class Tags extends Field
      */
     public function render()
     {
+        $value = Helper::array($this->value());
+
         if ($this->options instanceof \Closure) {
             $this->options(
-                $this->options->call($this->getFormModel(), $this->value, $this)
+                $this->options->call($this->getFormModel(), $value, $this)
             );
         }
 
@@ -169,9 +171,9 @@ class Tags extends Field
         });";
 
         if ($this->keyAsValue) {
-            $options = $this->value + $this->options;
+            $options = $value + $this->options;
         } else {
-            $options = array_unique(array_merge($this->value, $this->options));
+            $options = array_unique(array_merge($value, $this->options));
         }
 
         return parent::render()->with([

+ 2 - 2
src/Form/Field/Tree.php

@@ -109,13 +109,13 @@ class Tree extends Field
     protected function formatNodes()
     {
         $value = Helper::array(
-            old($this->column, $this->value ?: $this->getDefault())
+            old($this->column, $this->value())
         );
 
         $this->value = &$value;
 
         if ($this->nodes instanceof \Closure) {
-            $this->nodes = $this->nodes->call($this->getFormModel(), $this->value, $this);
+            $this->nodes = $this->nodes->call($this->getFormModel(), $this->value(), $this);
         }
 
         if (!$this->nodes) return;