Skip to content

Commit 116c95c

Browse files
committed
add support for formwidgets as fieldset internal fields
1 parent 9c885c0 commit 116c95c

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

modules/backend/formwidgets/FieldSet.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Backend\FormWidgets;
44

55
use ApplicationException;
6+
use Backend\Classes\FormField;
67
use Backend\Classes\FormWidgetBase;
78
use Backend\Widgets\Form;
89

@@ -71,14 +72,15 @@ protected function loadAssets()
7172
*/
7273
public function getFormFields(): array
7374
{
74-
$fields = $this->formWidget->getFields();
75+
return $this->formWidget->getFields();
76+
}
7577

76-
foreach ($fields as $field) {
77-
if ($this->model->hasRelation($field->fieldName)) {
78-
throw new ApplicationException(trans('backend::lang.fieldset.relation-not-supported', ['field' => $field->fieldName]));
79-
}
80-
}
81-
return $fields;
78+
/**
79+
* return an internal formwidget's formWidget
80+
*/
81+
public function getFormWidget($field): FormWidgetBase
82+
{
83+
return $this->formWidget->getFormWidget($field);
8284
}
8385

8486
/**
@@ -94,4 +96,12 @@ public function prepareVars()
9496
{
9597
$this->formWidget->previewMode = $this->previewMode;
9698
}
99+
100+
/**
101+
* @inheritDoc
102+
*/
103+
public function getSaveValue($value)
104+
{
105+
return $this->formWidget->getSaveValue($value);
106+
}
97107
}

modules/backend/lang/en/lang.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,6 @@
244244
'column_switch_true' => 'Yes',
245245
'column_switch_false' => 'No',
246246
],
247-
'fieldset' => [
248-
'relation-not-supported' => ':field: model relation fields are not supported by the fieldset formwidget',
249-
],
250247
'fileupload' => [
251248
'attachment' => 'Attachment',
252249
'help' => 'Add a title and description for this attachment.',

modules/backend/widgets/Form.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Backend\Classes\FormWidgetBase;
77
use Backend\Classes\WidgetBase;
88
use Backend\Classes\WidgetManager;
9+
use Backend\FormWidgets\FieldSet;
910
use BackendAuth;
1011
use Exception;
1112
use Form as FormHelper;
@@ -1222,20 +1223,21 @@ public function getSaveData(): array
12221223
}
12231224

12241225
// get nested widget fields that should be saved to the model
1225-
if (method_exists($widget, 'getFormFields')) {
1226+
if ($widget instanceof FieldSet) {
12261227
foreach ($widget->getFormFields() as $field) {
12271228
$parts = HtmlHelper::nameToArray($field->fieldName);
12281229
if (($value = $this->dataArrayGet($data, $parts)) !== null) {
1229-
/*
1230-
* Number fields should be converted to integers
1231-
*/
12321230
if ($field->type === 'number') {
12331231
$value = !strlen(trim($value)) ? null : (float) $value;
12341232
}
1233+
if ($field->type === 'widget') {
1234+
$value = $widget->getFormWidget($field->fieldName)->getSaveValue($value);
1235+
}
12351236

12361237
$this->dataArraySet($result, $parts, $value);
12371238
}
12381239
}
1240+
continue;
12391241
}
12401242

12411243
// Exclude fields that didn't provide any value

0 commit comments

Comments
 (0)