diff --git a/src/Form.php b/src/Form.php index 5252db2..156a745 100644 --- a/src/Form.php +++ b/src/Form.php @@ -3,7 +3,6 @@ namespace Uniform; use Kirby\Http\Url; -use ErrorException; use Kirby\Toolkit\Str; use Kirby\Http\Response; use Uniform\Guards\Guard; @@ -68,6 +67,17 @@ class Form extends BaseForm */ protected $success; + /** + * Hooks to be executed in different stages of form processing + * + * @var array + */ + protected $hooks = [ + 'success' => [], + 'error' => [], + 'always' => [], + ]; + /** * Create a new instance * @@ -209,6 +219,25 @@ public function action($action, $options = []) return $this; } + /** + * Register a hook + * + * @throws Exception If the hook event does not exist + * + * @param string $hook Hook event + * @param callable $callback Hook callback + * @return Form + */ + public function on(string $event, callable $fn): Form + { + if (! in_array($event, array_keys($this->hooks))) { + throw new Exception("Hook event '{$event}' does not exist."); + } + + $this->hooks[$event][] = $fn; + return $this; + } + /** * Redirect back (after actions have been performed). */ @@ -216,6 +245,13 @@ public function done() { $this->flash->set(self::FLASH_KEY_SUCCESS, $this->success); + foreach ($this->hooks[$this->success ? 'success' : 'error'] as $hook) { + $hook($this); + } + foreach ($this->hooks['always'] as $hook) { + $hook($this); + } + if ($this->shouldRedirect) { die(Response::redirect(Url::last())); } else {