From 7b0f33e9b3074217c5a392fbe2c6351c21bb2455 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 5 Nov 2016 09:53:23 +1100 Subject: [PATCH] Adds an isNested flag to Form widget This is useful when a form renders another form inside, specifically the repeater. In these cases the model and data will diverge, and it also provides an opportunity to not apply extension logic to nested form fields. Fixes #2257 --- modules/backend/formwidgets/Repeater.php | 9 +++++++-- modules/backend/widgets/Form.php | 11 +++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/modules/backend/formwidgets/Repeater.php b/modules/backend/formwidgets/Repeater.php index 70205ff15..42cc754c5 100644 --- a/modules/backend/formwidgets/Repeater.php +++ b/modules/backend/formwidgets/Repeater.php @@ -114,7 +114,9 @@ class Repeater extends FormWidgetBase $itemIndexes = post(self::INDEX_PREFIX.$this->formField->getName(false), $loadValue); - if (!is_array($itemIndexes)) return; + if (!is_array($itemIndexes)) { + return; + } foreach ($itemIndexes as $itemIndex) { $this->makeItemFormWidget($itemIndex); @@ -125,13 +127,16 @@ class Repeater extends FormWidgetBase protected function makeItemFormWidget($index = 0) { $loadValue = $this->getLoadValue(); - if (!is_array($loadValue)) $loadValue = []; + if (!is_array($loadValue)) { + $loadValue = []; + } $config = $this->makeConfig($this->form); $config->model = $this->model; $config->data = array_get($loadValue, $index, []); $config->alias = $this->alias . 'Form'.$index; $config->arrayName = $this->formField->getName().'['.$index.']'; + $config->isNested = true; $widget = $this->makeWidget('Backend\Widgets\Form', $config); $widget->bindToController(); diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index 6f246139e..c9ec6cdd1 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -63,6 +63,12 @@ class Form extends WidgetBase */ public $arrayName; + /** + * @var bool Used to flag that this form is being rendered as part of another form, + * a good indicator to expect that the form model and dataset values will differ. + */ + public $isNested = false; + // // Object properties // @@ -124,8 +130,9 @@ class Form extends WidgetBase 'secondaryTabs', 'model', 'data', - 'arrayName', 'context', + 'arrayName', + 'isNested', ]); $this->widgetManager = WidgetManager::instance(); @@ -1070,7 +1077,7 @@ class Form extends WidgetBase ])); } - $fieldOptions = $this->model->$fieldOptions($field->value, $field->fieldName); + $fieldOptions = $this->model->$fieldOptions($field->value, $field->fieldName, $this->data); } return $fieldOptions;