picea-ui/src/Form/UiForm.php
Dave Mc Nicoll 657ceb2863 - Added multi-part by default to forms
- Fixed some bug within FormContext and FormHandler.
2020-02-17 08:17:36 -05:00

68 lines
1.9 KiB
PHP

<?php
namespace Picea\Ui\Form;
use Picea\Ui\Common\UiElement,
Picea\Extension\Extension,
Picea\Extension\ExtensionTrait;
class UiForm extends UiElement implements Extension {
use ExtensionTrait;
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 string $tag = "form";
public array $attributes = [
'class' => 'ui-form',
];
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string
{
switch($token) {
case 'ui.endform':
return "</form>";
case "ui.form.get":
$method = "get";
break;
case "ui.form.patch":
$method = "patch";
break;
case "ui.form.delete":
$method = "delete";
break;
case "ui.form.put":
$method = "put";
break;
case "ui.form.post":
$method = "post";
break;
}
$method ??= $this->defaultMethod;
return "<?php echo ( new \\" . static::class . "() )->buildHtml('$method', $arguments) ?>";
}
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" ]) );
$this->attributes([ 'enctype' => "multipart/form-data" ]);
}
return $this->render() . PHP_EOL;
}
}