- First WIP on form handling
This commit is contained in:
1 parent
cb2caf895e
commit
a8a99f1852
6 files changed
+173
-38
No files matched your search
+9
-38
@@ -16,6 +16,8 @@ class Form implements Extension {
|
||||
|
||||
public string $token;
|
||||
|
||||
public FormContext $formContext;
|
||||
|
||||
public ServerRequestInterface $request;
|
||||
|
||||
public function __construct(ServerRequestInterface $request, Context $context) {
|
||||
@@ -27,7 +29,11 @@ class Form implements Extension {
|
||||
|
||||
public function register(Context $context) : void
|
||||
{
|
||||
$this->formContext = new FormContext($this->request);
|
||||
|
||||
$context->pushFunction("form", [ $this, 'formClass' ]);
|
||||
|
||||
#$context->
|
||||
}
|
||||
|
||||
public function form_csrf(string $field, string $value) {
|
||||
@@ -46,43 +52,8 @@ class Form implements Extension {
|
||||
return $value;
|
||||
}
|
||||
|
||||
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, /* 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
|
||||
{
|
||||
return in_array($this->request->getMethod(), [
|
||||
"DELETE", "PATCH", "POST", "PUT",
|
||||
]);
|
||||
}
|
||||
|
||||
protected function honeyPot() : bool
|
||||
{
|
||||
$this->request->getServerParams();
|
||||
}
|
||||
};
|
||||
public function formClass(FormInterface $form, ? FormContext $formContext = null)
|
||||
{
|
||||
return new FormHandler($this->request, $form, $formContext ?: $this->formContext);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user