Add hidden "loaded" flag for repeaters

This allows the repeater to retrieve the load value
from the model only on initialisation. Any further
requests to the repeater (ie. AJAX requests) should
use the POST data.
This commit is contained in:
Ben Thomson 2019-04-11 17:57:27 +08:00
parent ac98f70a25
commit ee2b53fe71
2 changed files with 29 additions and 3 deletions

View File

@ -77,6 +77,13 @@ class Repeater extends FormWidgetBase
protected $groupDefinitions = [];
/**
* Determines if repeater has been initialised previously
*
* @var boolean
*/
protected $loaded = false;
/**
* @inheritDoc
*/
@ -95,6 +102,11 @@ class Repeater extends FormWidgetBase
$this->previewMode = true;
}
// Check for loaded flag in POST
if ((bool) post($this->alias . '_loaded') === true) {
$this->loaded = true;
}
$fieldName = $this->formField->getName(false);
$this->processGroupMode();
@ -197,8 +209,17 @@ class Repeater extends FormWidgetBase
*/
protected function processItems()
{
$currentValue = ($this->loaded === true)
? post($this->formField->getName())
: $this->getLoadValue();
if ($currentValue === null) {
$this->indexCount = 0;
$this->formWidgets = [];
return;
}
$groupMap = [];
$currentValue = post($this->formField->getName(), $this->getLoadValue());
// Ensure that the minimum number of items are preinitialized
// ONLY DONE WHEN NOT IN GROUP MODE
@ -269,7 +290,10 @@ class Repeater extends FormWidgetBase
*/
protected function getValueFromIndex($index)
{
$value = post($this->formField->fieldName, $this->getLoadValue());
$value = ($this->loaded === true)
? post($this->formField->getName())
: $this->getLoadValue();
if (!is_array($value)) {
$value = [];
}
@ -291,7 +315,7 @@ class Repeater extends FormWidgetBase
$this->vars['widget'] = $this->makeItemFormWidget($this->indexCount, $groupCode);
$this->vars['indexValue'] = $this->indexCount;
$itemContainer = '@#'.$this->getId('items');
$itemContainer = '@#' . $this->getId('items');
// Increase index count after item is created
++$this->indexCount;

View File

@ -34,6 +34,8 @@
</a>
<?php endif ?>
</div>
<input type="hidden" name="<?= $this->alias; ?>_loaded" value="1">
<?php endif ?>
<script type="text/template" data-group-palette-template>