diff --git a/src/Form/UiDatetime.php b/src/Form/UiDatetime.php new file mode 100644 index 0000000..bf9aeeb --- /dev/null +++ b/src/Form/UiDatetime.php @@ -0,0 +1,13 @@ + "ui-datetime", + 'type' => "datetime-local", + ]; +} diff --git a/src/Form/UiForm.php b/src/Form/UiForm.php index 9e2b8e7..77119c0 100644 --- a/src/Form/UiForm.php +++ b/src/Form/UiForm.php @@ -23,7 +23,7 @@ class UiForm extends UiElement implements Extension { { switch($token) { case 'ui.endform': - return ( new UiHidden() )->attributes([ 'name' =>"picea-csrf-protection", 'value' => "abcdefg" ])->render() . ""; + return ""; case "ui.form.get": $method = "get"; @@ -49,13 +49,18 @@ class UiForm extends UiElement implements Extension { $method ??= $this->defaultMethod; return "buildHtml('$method', $arguments) ?>"; } - - public function buildHtml(string $method, string $action, array $attributes = []) : string + public function buildHtml(string $method = "get", string $action = "", array $attributes = []) : string { + $method = strtolower($method); + $this->option('tag-type', 'single'); $this->attributes([ 'method' => $method, 'action' => $action ] + $attributes); + if ( $method !== "get" ) { + $this->append( ( new UiHidden() )->attributes([ 'name' =>"picea-csrf-protection", 'value' => "abcdefg" ]) ); + } + return $this->render() . PHP_EOL; } } \ No newline at end of file diff --git a/src/Form/UiSelect.php b/src/Form/UiSelect.php index e5e5663..ac11db2 100644 --- a/src/Form/UiSelect.php +++ b/src/Form/UiSelect.php @@ -38,7 +38,10 @@ class UiSelect extends UiElement implements Extension { protected function setList(array $list, $selected, bool $strict = true) : void { foreach($list as $key => $item) { + $isSelected = $this->isSelected($key, $selected, $strict); + if ($item instanceof UiElement) { + $this->append($item); continue; @@ -48,11 +51,11 @@ class UiSelect extends UiElement implements Extension { $obj->text($key); foreach($item as $subKey => $subItem) { - $obj->append($this->createOption($subItem, $subKey, $strict ? $subKey === $selected : $subKey == $selected)); + $obj->append($this->createOption($subItem, $subKey, $isSelected)); } } else { - $obj = $this->createOption($item, $key, $strict ? $key === $selected : $key == $selected); + $obj = $this->createOption($item, $key, $isSelected); } $this->append($obj); @@ -71,4 +74,16 @@ class UiSelect extends UiElement implements Extension { return $option; } + + protected function isSelected($check, $value, bool $strict = true) : bool + { + if (false !== ( $f = filter_var($value, FILTER_VALIDATE_INT) ) ) { + $value = $f; + } + elseif (false !== ( $f = filter_var($value, FILTER_VALIDATE_FLOAT) ) ) { + $value = $f; + } + + return $strict ? $check === $value : $check == $value; + } } diff --git a/src/Method/Form.php b/src/Method/Form.php index bec4435..845587e 100644 --- a/src/Method/Form.php +++ b/src/Method/Form.php @@ -16,7 +16,10 @@ class Form implements Extension { public string $token; - public function __construct(Context $context) { + public ServerRequestInterface $request; + + public function __construct(ServerRequestInterface $request, Context $context) { + $this->request = $request; $this->register($context); } @@ -43,17 +46,30 @@ class Form implements Extension { return $value; } - public function formClass(ServerRequestInterface $request) { - return new class($request) { + public function formClass($form) { + return new class($this->request, $form) { public bool $sent = false; protected ServerRequestInterface $request; + + protected /* FormInterface */ $form; - public function __construct(ServerRequestInterface $request) + public function __construct(ServerRequestInterface $request, /* FormInterface */ $form) { $this->request = $request; $this->sent = $this->requestSent(); + $this->form = $form; + } + + protected function initialize() { + $this->form->prepare(); + + if ( $this->sent ) { + if ( $this->form->validate() ) { + $this->form->save(); + } + } } protected function requestSent() : bool diff --git a/src/Ui.php b/src/Ui.php index 379f6b0..1a94207 100644 --- a/src/Ui.php +++ b/src/Ui.php @@ -13,6 +13,7 @@ class Ui { $compiler->registerExtension(new Form\UiCheckbox()); $compiler->registerExtension(new Form\UiColor()); $compiler->registerExtension(new Form\UiDate()); + $compiler->registerExtension(new Form\UiDatetime()); $compiler->registerExtension(new Form\UiEmail()); $compiler->registerExtension(new Form\UiFile()); $compiler->registerExtension(new Form\UiHidden());