Fix setting default values for repeater in update context (#4098)

Credit to @CptMeatball
This commit is contained in:
Antonie Hogewoning 2019-01-31 20:50:21 +01:00 committed by Luke Towers
parent 331c8f4383
commit 16ca521cd0
2 changed files with 14 additions and 1 deletions

View File

@ -268,6 +268,7 @@ class Repeater extends FormWidgetBase
$config->alias = $this->alias . 'Form'.$index; $config->alias = $this->alias . 'Form'.$index;
$config->arrayName = $this->getFieldName().'['.$index.']'; $config->arrayName = $this->getFieldName().'['.$index.']';
$config->isNested = true; $config->isNested = true;
$config->enableDefaults = self::$onAddItemCalled;
$widget = $this->makeWidget('Backend\Widgets\Form', $config); $widget = $this->makeWidget('Backend\Widgets\Form', $config);
$widget->bindToController(); $widget->bindToController();

View File

@ -1071,7 +1071,7 @@ class Form extends WidgetBase
$field = $this->allFields[$field]; $field = $this->allFields[$field];
} }
$defaultValue = !$this->model->exists $defaultValue = $this->shouldFetchDefaultValues()
? $field->getDefaultFromData($this->data) ? $field->getDefaultFromData($this->data)
: null; : 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 * Returns a HTML encoded value containing the other fields this
* field depends on * field depends on