Added option to fill the form by adding the data parameter to the options.
This commit is contained in:
parent
eec70787ef
commit
1fb5d6320d
2 changed files with 82 additions and 4 deletions
70
lib/Lewp/Forms/Torunn.php
Normal file
70
lib/Lewp/Forms/Torunn.php
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lewp\Forms;
|
||||
|
||||
class Torunn
|
||||
{
|
||||
|
||||
private static $direct_inputs = [
|
||||
'input'
|
||||
];
|
||||
|
||||
private static $indirect_inputs = [
|
||||
'select'
|
||||
];
|
||||
|
||||
public static function fill(\DOMNode $form, array $data_array)
|
||||
{
|
||||
self::traverse($form, $data_array);
|
||||
}
|
||||
|
||||
private static function traverse($node, array &$data, bool $recursive = true)
|
||||
{
|
||||
$children = $node->childNodes;
|
||||
$children_count = count($children);
|
||||
for ($i = 0; $i < $children_count; ++$i) {
|
||||
$child = $children[$i];
|
||||
|
||||
if (in_array($child->tagName, self::$direct_inputs)) {
|
||||
self::fillDirectInput($child, array_shift($data) ?? '');
|
||||
continue;
|
||||
//$child->setAttribute('value', array_shift($data) ?? 'empty');
|
||||
}
|
||||
|
||||
if ($child->tagName === 'select') {
|
||||
self::fillSelectElement($child, array_shift($data) ?? '');
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($recursive) {
|
||||
self::traverse($child, $data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static function fillDirectInput($node, $value)
|
||||
{
|
||||
if ($value === '') {
|
||||
return;
|
||||
}
|
||||
$node->setAttribute('value', $value);
|
||||
}
|
||||
|
||||
private static function fillSelectElement($node, $value)
|
||||
{
|
||||
if ($value === '') {
|
||||
return;
|
||||
}
|
||||
$children = $node->childNodes;
|
||||
foreach ($children as $child) {
|
||||
if (($child->tagName === 'option')
|
||||
&& ($child->getAttribute('value') === $value)
|
||||
) {
|
||||
$child->setAttribute('selected', 'selected');
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
16
module.php
16
module.php
|
|
@ -1,12 +1,14 @@
|
|||
<?php
|
||||
|
||||
use Lewp\{Module, Resolve, VarFolder};
|
||||
use Lewp\Forms\Torunn;
|
||||
|
||||
return new class extends Module
|
||||
{
|
||||
const OPTIONS_ELEMENTS = 'elements';
|
||||
const OPTIONS_ELEMENT = 'element';
|
||||
const OPTIONS_LABEL = 'label';
|
||||
const OPTIONS_DATA = 'data';
|
||||
|
||||
// element specific
|
||||
const OPTIONS_SHOW_PLACEHOLDER = 'show_placeholder';
|
||||
|
|
@ -154,9 +156,6 @@ return new class extends Module
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief IMPORTANT: Needs to be called AFTER setupForm to work correctly!
|
||||
*/
|
||||
private function isSubmitted()
|
||||
{
|
||||
return count($this->collectFormKeys()) > 0;
|
||||
|
|
@ -193,6 +192,10 @@ return new class extends Module
|
|||
|
||||
public function run(array $options = []) : bool
|
||||
{
|
||||
// add default options
|
||||
$options += [
|
||||
self::OPTIONS_DATA => [],
|
||||
];
|
||||
// SETUP FORM ATTRIBUTES
|
||||
$this->setAttribute('method', $options[self::OPTIONS_METHOD] ?? 'POST');
|
||||
$this->setAttribute(
|
||||
|
|
@ -200,8 +203,13 @@ return new class extends Module
|
|||
$this->processAction($options[self::OPTIONS_ACTION]) ?? '/'
|
||||
);
|
||||
|
||||
|
||||
$this->setupForm($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]);
|
||||
}
|
||||
|
||||
if ($this->isSubmitted()) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue