Added support to make torunn a subform using the is_subform option parameter.

This commit is contained in:
Lewin Probst 2020-03-22 18:12:04 +01:00
parent 4fef660d7c
commit 3c1da15e99
2 changed files with 20 additions and 11 deletions

View file

@ -1,5 +1,4 @@
<?php
return [
"element_type_module" => "form",
];

View file

@ -10,6 +10,7 @@ return new class extends Module
const OPTIONS_LABEL = 'label';
const OPTIONS_DATA = 'data';
const OPTIONS_CALLBACK_ON_SUBMIT = 'on_submit';
const OPTIONS_SUBFORM = 'is_subform';
// element specific
const OPTIONS_SHOW_PLACEHOLDER = 'show_placeholder';
@ -143,10 +144,10 @@ return new class extends Module
return $label;
}
private function setupForm(array $elements)
private function setupForm(\DOMNode $form, array $elements)
{
foreach ($elements as $element) {
$this->appendChild($this->createFormElementGroup($element));
$form->appendChild($this->createFormElementGroup($element));
}
}
@ -202,6 +203,7 @@ return new class extends Module
// add default options
$options += [
self::OPTIONS_DATA => [],
self::OPTIONS_SUBFORM => false,
self::OPTIONS_CALLBACK_ON_SUBMIT => function ($data) {
return true;
},
@ -216,18 +218,26 @@ return new class extends Module
return false;
}
// SETUP FORM ATTRIBUTES
$this->setAttribute('method', $options[self::OPTIONS_METHOD] ?? 'POST');
$this->setAttribute(
'action',
$this->processAction($options[self::OPTIONS_ACTION]) ?? '/'
);
// setup form attributes if required
if ($options[self::OPTIONS_SUBFORM]) {
$form = $this;
} else {
$form = $this->createAndSetupElement(
'form',
'',
[
'method' => $options[self::OPTIONS_METHOD] ?? 'POST',
'action' => $this->processAction($options[self::OPTIONS_ACTION]) ?? '/',
]
);
$this->appendChild($form);
}
$this->setupForm($options[self::OPTIONS_ELEMENTS]);
$this->setupForm($form, $options[self::OPTIONS_ELEMENTS]);
// if data is present, filter the correct keys and fill the form
if (!empty($options[self::OPTIONS_DATA])) {
Torunn::fill($this, $options[self::OPTIONS_DATA]);
Torunn::fill($form, $options[self::OPTIONS_DATA]);
}
if ($this->isSubmitted()) {