- Fixed UiSelect's selected item method

- Added a new DateTime (WIP from firefox still)
This commit is contained in:
Dave Mc Nicoll 2020-01-31 16:37:17 -05:00
parent e2c1c7ac39
commit cb2caf895e
5 changed files with 59 additions and 9 deletions

13
src/Form/UiDatetime.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace Picea\Ui\Form;
class UiDatetime extends UiInput {
public string $token = "ui.datetime";
public array $attributes = [
'class' => "ui-datetime",
'type' => "datetime-local",
];
}

View File

@ -23,7 +23,7 @@ class UiForm extends UiElement implements Extension {
{ {
switch($token) { switch($token) {
case 'ui.endform': case 'ui.endform':
return ( new UiHidden() )->attributes([ 'name' =>"picea-csrf-protection", 'value' => "abcdefg" ])->render() . "</form>"; return "</form>";
case "ui.form.get": case "ui.form.get":
$method = "get"; $method = "get";
@ -50,12 +50,17 @@ class UiForm extends UiElement implements Extension {
return "<?php echo ( new \\" . static::class . "() )->buildHtml('$method', $arguments) ?>"; return "<?php echo ( new \\" . static::class . "() )->buildHtml('$method', $arguments) ?>";
} }
public function buildHtml(string $method = "get", string $action = "", array $attributes = []) : string
public function buildHtml(string $method, string $action, array $attributes = []) : string
{ {
$method = strtolower($method);
$this->option('tag-type', 'single'); $this->option('tag-type', 'single');
$this->attributes([ 'method' => $method, 'action' => $action ] + $attributes); $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; return $this->render() . PHP_EOL;
} }
} }

View File

@ -38,7 +38,10 @@ class UiSelect extends UiElement implements Extension {
protected function setList(array $list, $selected, bool $strict = true) : void protected function setList(array $list, $selected, bool $strict = true) : void
{ {
foreach($list as $key => $item) { foreach($list as $key => $item) {
$isSelected = $this->isSelected($key, $selected, $strict);
if ($item instanceof UiElement) { if ($item instanceof UiElement) {
$this->append($item); $this->append($item);
continue; continue;
@ -48,11 +51,11 @@ class UiSelect extends UiElement implements Extension {
$obj->text($key); $obj->text($key);
foreach($item as $subKey => $subItem) { foreach($item as $subKey => $subItem) {
$obj->append($this->createOption($subItem, $subKey, $strict ? $subKey === $selected : $subKey == $selected)); $obj->append($this->createOption($subItem, $subKey, $isSelected));
} }
} }
else { else {
$obj = $this->createOption($item, $key, $strict ? $key === $selected : $key == $selected); $obj = $this->createOption($item, $key, $isSelected);
} }
$this->append($obj); $this->append($obj);
@ -71,4 +74,16 @@ class UiSelect extends UiElement implements Extension {
return $option; 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;
}
} }

View File

@ -16,7 +16,10 @@ class Form implements Extension {
public string $token; public string $token;
public function __construct(Context $context) { public ServerRequestInterface $request;
public function __construct(ServerRequestInterface $request, Context $context) {
$this->request = $request;
$this->register($context); $this->register($context);
} }
@ -43,17 +46,30 @@ class Form implements Extension {
return $value; return $value;
} }
public function formClass(ServerRequestInterface $request) { public function formClass($form) {
return new class($request) { return new class($this->request, $form) {
public bool $sent = false; public bool $sent = false;
protected ServerRequestInterface $request; protected ServerRequestInterface $request;
public function __construct(ServerRequestInterface $request) protected /* FormInterface */ $form;
public function __construct(ServerRequestInterface $request, /* FormInterface */ $form)
{ {
$this->request = $request; $this->request = $request;
$this->sent = $this->requestSent(); $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 protected function requestSent() : bool

View File

@ -13,6 +13,7 @@ class Ui {
$compiler->registerExtension(new Form\UiCheckbox()); $compiler->registerExtension(new Form\UiCheckbox());
$compiler->registerExtension(new Form\UiColor()); $compiler->registerExtension(new Form\UiColor());
$compiler->registerExtension(new Form\UiDate()); $compiler->registerExtension(new Form\UiDate());
$compiler->registerExtension(new Form\UiDatetime());
$compiler->registerExtension(new Form\UiEmail()); $compiler->registerExtension(new Form\UiEmail());
$compiler->registerExtension(new Form\UiFile()); $compiler->registerExtension(new Form\UiFile());
$compiler->registerExtension(new Form\UiHidden()); $compiler->registerExtension(new Form\UiHidden());