From 16ca521cd08985d8cbb6fca8cbeff6a52a693055 Mon Sep 17 00:00:00 2001 From: Antonie Hogewoning Date: Thu, 31 Jan 2019 20:50:21 +0100 Subject: [PATCH] Fix setting default values for repeater in update context (#4098) Credit to @CptMeatball --- modules/backend/formwidgets/Repeater.php | 1 + modules/backend/widgets/Form.php | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/modules/backend/formwidgets/Repeater.php b/modules/backend/formwidgets/Repeater.php index 6c0cdf269..bb490ddd1 100644 --- a/modules/backend/formwidgets/Repeater.php +++ b/modules/backend/formwidgets/Repeater.php @@ -268,6 +268,7 @@ class Repeater extends FormWidgetBase $config->alias = $this->alias . 'Form'.$index; $config->arrayName = $this->getFieldName().'['.$index.']'; $config->isNested = true; + $config->enableDefaults = self::$onAddItemCalled; $widget = $this->makeWidget('Backend\Widgets\Form', $config); $widget->bindToController(); diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index 24025bf14..b7362cab3 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -1071,7 +1071,7 @@ class Form extends WidgetBase $field = $this->allFields[$field]; } - $defaultValue = !$this->model->exists + $defaultValue = $this->shouldFetchDefaultValues() ? $field->getDefaultFromData($this->data) : null; @@ -1081,6 +1081,18 @@ class Form extends WidgetBase ); } + /** + * Checks if default values should be taken from data. + * This should be done when model exists or when explicitly configured + */ + protected function shouldFetchDefaultValues() { + $enableDefaults = object_get($this->config, 'enableDefaults'); + if ($enableDefaults === false) { + return false; + } + return !$this->model->exists || $enableDefaults; + } + /** * Returns a HTML encoded value containing the other fields this * field depends on