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
This commit is contained in:
Samuel Georges 2016-11-05 09:53:23 +11:00
parent 348dd85338
commit 7b0f33e9b3
2 changed files with 16 additions and 4 deletions

View File

@ -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();

View File

@ -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;