From 6cdfaeb8fbb665571feaf89e154afc472d3009fa Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Thu, 18 Apr 2019 11:43:22 +0300 Subject: [PATCH] Improve `FormTrait` backwards compatibility with existing forms --- CHANGELOG.md | 6 ++ system/src/Grav/Framework/Flex/FlexForm.php | 2 +- .../Form/Interfaces/FormInterface.php | 5 ++ .../Grav/Framework/Form/Traits/FormTrait.php | 86 ++++++++++++------- 4 files changed, 67 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 258c9c3ee..f890b47ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v1.6.7 +## mm/dd/2019 + +1. [](#improved) + * Improve `FormTrait` backwards compatibility with existing forms + # v1.6.6 ## 04/17/2019 diff --git a/system/src/Grav/Framework/Flex/FlexForm.php b/system/src/Grav/Framework/Flex/FlexForm.php index e3b9b42fa..7faad5227 100644 --- a/system/src/Grav/Framework/Flex/FlexForm.php +++ b/system/src/Grav/Framework/Flex/FlexForm.php @@ -54,7 +54,7 @@ class FlexForm implements FlexFormInterface $this->setObject($object); $this->setId($this->getName()); $this->setUniqueId(md5($uniqueId)); - $this->errors = []; + $this->messages = []; $this->submitted = false; $flash = $this->getFlash(); diff --git a/system/src/Grav/Framework/Form/Interfaces/FormInterface.php b/system/src/Grav/Framework/Form/Interfaces/FormInterface.php index 7bfd85fdc..d218e7282 100644 --- a/system/src/Grav/Framework/Form/Interfaces/FormInterface.php +++ b/system/src/Grav/Framework/Form/Interfaces/FormInterface.php @@ -140,6 +140,11 @@ interface FormInterface extends RenderInterface, \Serializable */ public function isValid(): bool; + /** + * @return string + */ + public function getError(): ?string; + /** * @return array */ diff --git a/system/src/Grav/Framework/Form/Traits/FormTrait.php b/system/src/Grav/Framework/Form/Traits/FormTrait.php index 41e22dd24..bcd63bdd4 100644 --- a/system/src/Grav/Framework/Form/Traits/FormTrait.php +++ b/system/src/Grav/Framework/Form/Traits/FormTrait.php @@ -30,6 +30,13 @@ use Twig\TemplateWrapper; */ trait FormTrait { + /** @var string */ + public $status = 'success'; + /** @var string */ + public $message; + /** @var string[] */ + public $messages = []; + /** @var string */ private $name; /** @var string */ @@ -38,8 +45,6 @@ trait FormTrait private $uniqueid; /** @var bool */ private $submitted; - /** @var string[] */ - private $errors; /** @var Data|object|null */ private $data; /** @var array|UploadedFileInterface[] */ @@ -164,12 +169,24 @@ trait FormTrait */ public function handleRequest(ServerRequestInterface $request): FormInterface { + // Set current form to be active. + $grav = Grav::instance(); + $forms = $grav['forms'] ?? null; + if ($forms) { + $forms->setActiveForm($this); + + /** @var Twig $twig */ + $twig = $grav['twig']; + $twig->twig_vars['form'] = $this; + + } + try { [$data, $files] = $this->parseRequest($request); $this->submit($data, $files); } catch (\Exception $e) { - $this->errors[] = $e->getMessage(); + $this->setError($e->getMessage()); } return $this; @@ -191,12 +208,17 @@ trait FormTrait public function isValid(): bool { - return !$this->errors; + return $this->status === 'success'; + } + + public function getError(): ?string + { + return !$this->isValid() ? $this->message : null; } public function getErrors(): array { - return $this->errors; + return !$this->isValid() ? $this->messages : []; } public function isSubmitted(): bool @@ -206,7 +228,7 @@ trait FormTrait public function validate(): bool { - if ($this->errors) { + if (!$this->isValid()) { return false; } @@ -214,19 +236,14 @@ trait FormTrait $this->validateData($this->data); $this->validateUploads($this->getFiles()); } catch (ValidationException $e) { - $list = []; - foreach ($e->getMessages() as $field => $errors) { - $list[] = $errors; - } - $list = array_merge(...$list); - $this->errors = $list; + $this->setErrors($e->getMessages()); } catch (\Exception $e) { - $this->errors[] = $e->getMessage(); + $this->setError($e->getMessage()); } $this->filterData($this->data); - return empty($this->errors); + return $this->isValid(); } /** @@ -252,7 +269,7 @@ trait FormTrait $this->submitted = true; } catch (\Exception $e) { - $this->errors[] = $e->getMessage(); + $this->setError($e->getMessage()); } return $this; @@ -265,7 +282,9 @@ trait FormTrait $this->data = null; $this->files = []; - $this->errors = []; + $this->status = 'success'; + $this->message = null; + $this->messages = []; $this->submitted = false; $this->flash = null; } @@ -310,7 +329,6 @@ trait FormTrait } /** - * Get form flash object. * * @return FormFlash @@ -376,16 +394,6 @@ trait FormTrait $this->flash = null; } - /** - * Set all errors. - * - * @param array $errors - */ - protected function setErrors(array $errors): void - { - $this->errors = array_merge($this->errors, $errors); - } - /** * Set a single error. * @@ -393,7 +401,19 @@ trait FormTrait */ protected function setError(string $error): void { - $this->errors[] = $error; + $this->status = 'error'; + $this->message = $error; + } + + /** + * Set all errors. + * + * @param array $errors + */ + protected function setErrors(array $errors): void + { + $this->status = 'error'; + $this->messages = $errors; } /** @@ -563,7 +583,7 @@ trait FormTrait $value = json_decode($value, true); if ($value === null && json_last_error() !== JSON_ERROR_NONE) { unset($data[$key]); - $this->errors[] = "Badly encoded JSON data (for {$key}) was sent to the form"; + $this->setError("Badly encoded JSON data (for {$key}) was sent to the form"); } } } @@ -583,7 +603,9 @@ trait FormTrait 'id' => $this->id, 'uniqueid' => $this->uniqueid, 'submitted' => $this->submitted, - 'errors' => $this->errors, + 'status' => $this->status, + 'message' => $this->message, + 'messages' => $this->messages, 'data' => $data, 'files' => $this->files, ]; @@ -598,7 +620,9 @@ trait FormTrait $this->id = $data['id']; $this->uniqueid = $data['uniqueid']; $this->submitted = $data['submitted'] ?? false; - $this->errors = $data['errors'] ?? []; + $this->status = $data['status'] ?? 'success'; + $this->message = $data['message'] ?? null; + $this->messages = $data['messages'] ?? []; $this->data = isset($data['data']) ? new Data($data['data'], $this->getBlueprint()) : null; $this->files = $data['files'] ?? []; }