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 <?php
return [ return [
"element_type_module" => "form",
]; ];

View file

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