Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form submit handling #81

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
119 changes: 55 additions & 64 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class Form extends BaseHtmlElement

public const ON_ELEMENT_REGISTERED = 'elementRegistered';
public const ON_ERROR = 'error';
public const ON_REQUEST = 'request';
public const ON_SUBMIT = 'submit';
/** @deprecated Use {@link ON_SUBMIT} instead. */
public const ON_SUCCESS = 'success';
public const ON_SENT = 'sent';
public const ON_VALIDATE = 'validate';

/** @var string Form submission URL */
Expand All @@ -41,9 +41,6 @@ class Form extends BaseHtmlElement
/** @var ServerRequestInterface The server request being processed */
protected $request;

/** @var string */
protected $redirectUrl;

protected $tag = 'form';

/**
Expand Down Expand Up @@ -152,51 +149,16 @@ public function getRequest()
return $this->request;
}

public function setRequest($request)
{
$this->request = $request;
$this->emit(Form::ON_REQUEST, [$request]);

return $this;
}

/**
* Get the url to redirect to on success
*
* @return string
*/
public function getRedirectUrl()
{
return $this->redirectUrl;
}

/**
* Set the url to redirect to on success
*
* @param string $url
*
* @return $this
*/
public function setRedirectUrl($url)
{
$this->redirectUrl = $url;

return $this;
}

/**
* @param ServerRequestInterface $request
*
* @return $this
*/
public function handleRequest(ServerRequestInterface $request)
{
$this->setRequest($request);
$this->request = $request;

if (! $this->hasBeenSent()) {
// Always assemble
$this->ensureAssembled();

return $this;
}

Expand All @@ -212,29 +174,46 @@ public function handleRequest(ServerRequestInterface $request)
default:
$params = [];
}

$this->populate($params);

// Assemble after populate in order to conditionally provide form elements
$this->ensureAssembled();

if ($this->hasBeenSubmitted()) {
if ($this->isValid()) {
try {
$this->emit(Form::ON_SENT, [$this]);
$this->onSuccess();
$this->emitOnce(Form::ON_SUCCESS, [$this]);
} catch (Exception $e) {
$this->addMessage($e);
$this->onError();
$this->emit(Form::ON_ERROR, [$e, $this]);
}
$submitElement = $this->getPressedSubmitElement();
if (
! empty($this->submitElements)
&& $submitElement === null
) {
// If elements are registered for submission, but none have been pressed,
// the form was most likely submitted via auto-submit. In this case,
// we validate all elements that have a value, but do nothing else.
$this->validatePartial();

return $this;
}

// From here, the form is considered submitted because either one of the submit elements has been pressed
// or the form has been sent without a submit element being registered.
if (
$submitElement->getAttributes()->get('formnovalidate')->getValue() !== true
&& ! $this->isValid()
) {
$this->onError();

return $this;
}
try {
if ($submitElement === $this->getSubmitButton()) {
$this->onSuccess();
$this->emitOnce(Form::ON_SUCCESS, [$this]);
} else {
$this->onError();
$this->onSubmit($submitElement);
$this->emit(static::ON_SUBMIT, [$submitElement]);
}
} else {
$this->validatePartial();
$this->emit(Form::ON_SENT, [$this]);
} catch (Exception $e) {
$this->addMessage($e);
$this->onError();
$this->emit(Form::ON_ERROR, [$e, $this]);
}

return $this;
Expand All @@ -259,7 +238,8 @@ public function hasBeenSent()
/**
* Get whether the form has been submitted
*
* A form is submitted when it has been sent and when the primary submit button, if set, has been pressed.
* A form is considered submitted because either it has been sent by pressing one of the registered submit elements
* or the form has been sent without a submit element being registered.
* This method calls {@link hasBeenSent()} in order to detect whether the form has been sent.
*
* @return bool
Expand All @@ -270,11 +250,11 @@ public function hasBeenSubmitted()
return false;
}

if ($this->hasSubmitButton()) {
return $this->getSubmitButton()->hasBeenPressed();
if (empty($this->submitElements)) {
return true;
}

return true;
return $this->getPressedSubmitElement() !== null;
}

/**
Expand Down Expand Up @@ -335,8 +315,15 @@ public function validatePartial()

public function remove(ValidHtml $elementOrHtml)
{
if ($this->submitButton === $elementOrHtml) {
$this->submitButton = null;
if ($elementOrHtml instanceof FormSubmitElement) {
if ($this->submitButton === $elementOrHtml) {
$this->submitButton = null;
}

$key = array_search($elementOrHtml, $this->submitElements, true);
if ($key !== false) {
unset($this->submitElements[$key]);
}
}

$this->removeElement($elementOrHtml);
Expand All @@ -358,9 +345,13 @@ protected function onError()
}
}

protected function onSubmit(FormSubmitElement $submitElement)
{
}

/** @deprecated Use {@link onSubmit()} instead. */
protected function onSuccess()
{
// $this->redirectOnSuccess();
}

protected function onElementRegistered(FormElement $element)
Expand Down
2 changes: 0 additions & 2 deletions src/FormElement/FormElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,7 @@ public function isValidEvent($event)
{
return in_array($event, [
Form::ON_SUCCESS,
Form::ON_SENT,
Form::ON_ERROR,
Form::ON_REQUEST,
Form::ON_VALIDATE,
Form::ON_ELEMENT_REGISTERED,
]);
Expand Down