Use Form getSaveData method when saving form widgets within repeaters (#4160)
Credit to @bennothommo
This commit is contained in:
parent
13d710b161
commit
13a7cc915d
|
|
@ -202,15 +202,9 @@ class Repeater extends FormWidgetBase
|
||||||
/*
|
/*
|
||||||
* Give repeated form field widgets an opportunity to process the data.
|
* Give repeated form field widgets an opportunity to process the data.
|
||||||
*/
|
*/
|
||||||
foreach ($this->formWidgets as $field => $form) {
|
foreach ($value as $index => $data) {
|
||||||
foreach ($form->getFormWidgets() as $formField => $widget) {
|
if (isset($this->formWidgets[$index])) {
|
||||||
$parts = HtmlHelper::nameToArray($field . '[' . $formField . ']');
|
$value[$index] = $this->formWidgets[$index]->getSaveData();
|
||||||
|
|
||||||
$widgetValue = $widget->getSaveValue($this->dataArrayGet($value, $parts));
|
|
||||||
if (empty($widgetValue) || !count($widgetValue)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$this->dataArraySet($value, $parts, $widgetValue);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -421,83 +415,4 @@ class Repeater extends FormWidgetBase
|
||||||
{
|
{
|
||||||
return array_get($this->groupDefinitions, $groupCode.'.name');
|
return array_get($this->groupDefinitions, $groupCode.'.name');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Internal helper for method existence checks.
|
|
||||||
*
|
|
||||||
* @param object $object
|
|
||||||
* @param string $method
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
protected function objectMethodExists($object, $method)
|
|
||||||
{
|
|
||||||
if (method_exists($object, 'methodExists')) {
|
|
||||||
return $object->methodExists($method);
|
|
||||||
}
|
|
||||||
|
|
||||||
return method_exists($object, $method);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Variant to array_get() but preserves dots in key names.
|
|
||||||
*
|
|
||||||
* @param array $array
|
|
||||||
* @param array $parts
|
|
||||||
* @param null $default
|
|
||||||
* @return array|null
|
|
||||||
*/
|
|
||||||
protected function dataArrayGet(array $array, array $parts, $default = null)
|
|
||||||
{
|
|
||||||
if ($parts === null) {
|
|
||||||
return $array;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count($parts) === 1) {
|
|
||||||
$key = array_shift($parts);
|
|
||||||
if (isset($array[$key])) {
|
|
||||||
return $array[$key];
|
|
||||||
}
|
|
||||||
|
|
||||||
return $default;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($parts as $segment) {
|
|
||||||
if (!is_array($array) || !array_key_exists($segment, $array)) {
|
|
||||||
return $default;
|
|
||||||
}
|
|
||||||
|
|
||||||
$array = $array[$segment];
|
|
||||||
}
|
|
||||||
|
|
||||||
return $array;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Variant to array_set() but preserves dots in key names.
|
|
||||||
*
|
|
||||||
* @param array $array
|
|
||||||
* @param array $parts
|
|
||||||
* @param string $value
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
protected function dataArraySet(array &$array, array $parts, $value)
|
|
||||||
{
|
|
||||||
if ($parts === null) {
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (count($parts) > 1) {
|
|
||||||
$key = array_shift($parts);
|
|
||||||
|
|
||||||
if (!isset($array[$key]) || !is_array($array[$key])) {
|
|
||||||
$array[$key] = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
$array =& $array[$key];
|
|
||||||
}
|
|
||||||
|
|
||||||
$array[array_shift($parts)] = $value;
|
|
||||||
|
|
||||||
return $array;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1076,7 +1076,7 @@ class Form extends WidgetBase
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
return $field->getValueFromData(
|
return $field->getValueFromData(
|
||||||
$this->data,
|
$this->data,
|
||||||
is_string($defaultValue) ? trans($defaultValue) : $defaultValue
|
is_string($defaultValue) ? trans($defaultValue) : $defaultValue
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue