- Token name and options are now passed through parse() method
This commit is contained in:
1 parent
77d05ae051
commit
aba66479be
12 files changed
+114
-150
No files matched your search
+43
-28
@@ -11,7 +11,7 @@ class UiForm extends UiElement implements Extension {
|
||||
|
||||
public string $defaultMethod = "get";
|
||||
|
||||
public array $token = [ "ui:form", "ui:endform" /*, "ui:form.get", "ui:form.post", "ui:form.patch", "ui:form.delete", "ui:form.put"*/ ];
|
||||
public array $token = [ "ui:form", "ui:endform" ];
|
||||
|
||||
public string $tag = "form";
|
||||
|
||||
@@ -19,43 +19,53 @@ class UiForm extends UiElement implements Extension {
|
||||
'class' => 'ui-form',
|
||||
];
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string
|
||||
public function __construct(
|
||||
public bool $enctype = true,
|
||||
public bool $csrf = true,
|
||||
) {
|
||||
parent::__construct(null);
|
||||
}
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||
{
|
||||
$constructor = [];
|
||||
|
||||
switch($token) {
|
||||
case 'ui:endform':
|
||||
return "</form>";
|
||||
}
|
||||
|
||||
$opt = $context->tokenOptions($token);
|
||||
if (in_array('no-enctype', $options)) {
|
||||
$constructor[] = "enctype: false";
|
||||
}
|
||||
|
||||
if (in_array('get', $opt)) {
|
||||
if (in_array('no-csrf', $options)) {
|
||||
$constructor[] = "csrf: false";
|
||||
}
|
||||
|
||||
if (in_array('get', $options)) {
|
||||
$method = "get";
|
||||
}
|
||||
elseif (in_array('post', $opt)) {
|
||||
elseif (in_array('post', $options)) {
|
||||
$method = "post";
|
||||
}
|
||||
elseif (in_array('put', $opt)) {
|
||||
elseif (in_array('put', $options)) {
|
||||
$method = "put";
|
||||
}
|
||||
elseif (in_array('delete', $opt)) {
|
||||
elseif (in_array('delete', $options)) {
|
||||
$method = "delete";
|
||||
}
|
||||
elseif (in_array('patch', $opt)) {
|
||||
elseif (in_array('patch', $options)) {
|
||||
$method = "patch";
|
||||
}
|
||||
|
||||
$method ??= $this->defaultMethod;
|
||||
|
||||
$opt = var_export($opt, true);
|
||||
$opt = var_export($options, true);
|
||||
|
||||
return "<?php echo ( new \\" . static::class . "() )->parseOptions($opt)->buildHtml('$method', $arguments) ?>";
|
||||
}
|
||||
$constructor = implode(',', $constructor);
|
||||
|
||||
public function parseOptions($options) : self
|
||||
{
|
||||
$this->options = $options;
|
||||
|
||||
return $this;
|
||||
return "<?php echo ( new \\" . static::class . "($constructor) )->buildHtml('$method', $arguments) ?>";
|
||||
}
|
||||
|
||||
public function buildHtml(string $method = "get", string $name = "", string $action = "", array $attributes = []) : string
|
||||
@@ -72,21 +82,26 @@ class UiForm extends UiElement implements Extension {
|
||||
$this->attributes([ 'method' => $method, 'action' => $action ] + $attributes);
|
||||
|
||||
if ( $method !== "get" ) {
|
||||
$token = md5( $name . microtime());
|
||||
$key = "picea-ui:form:{$name}";
|
||||
|
||||
if (count($_SESSION[$key] ?? []) > 100) {
|
||||
array_shift($_SESSION[$key]);
|
||||
if ($this->csrf) {
|
||||
$token = md5($name . microtime());
|
||||
$key = "picea-ui:form:{$name}";
|
||||
|
||||
if (count($_SESSION[$key] ?? []) > 100) {
|
||||
array_shift($_SESSION[$key]);
|
||||
}
|
||||
|
||||
$_SESSION[$key][] = $token;
|
||||
|
||||
$this->append((new UiHidden())->attributes([
|
||||
'name' => "picea-ui-form[$name]",
|
||||
'value' => $token,
|
||||
]));
|
||||
}
|
||||
|
||||
$_SESSION[$key][] = $token;
|
||||
|
||||
$this->append( ( new UiHidden() )->attributes([
|
||||
'name' => "picea-ui-form[$name]",
|
||||
'value' => $token,
|
||||
]));
|
||||
|
||||
$this->attributes([ 'enctype' => "multipart/form-data" ]);
|
||||
if ($this->enctype) {
|
||||
$this->attributes([ 'enctype' => "multipart/form-data" ]);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render() . PHP_EOL;
|
||||
|
||||
Reference in new issue
Block a user