From 53216631a65e5d3a68a0f9b7dcd8bd3745f398ec Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Thu, 11 Jul 2019 15:24:35 +0300 Subject: [PATCH] * Fixed `FlexForm` to allow multiple form instances with non-existing objects --- CHANGELOG.md | 1 + system/src/Grav/Framework/Flex/FlexForm.php | 34 +++++++++- .../src/Grav/Framework/Flex/FlexFormFlash.php | 68 +++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 system/src/Grav/Framework/Flex/FlexFormFlash.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cf9529b9..9c4406b37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Make `Route` objects immutable 1. [](#bugfix) * Fixed `Form` not to use deleted flash object until the end of the request fixing issues with reset + * Fixed `FlexForm` to allow multiple form instances with non-existing objects # v1.7.0-beta.4 ## 07/01/2019 diff --git a/system/src/Grav/Framework/Flex/FlexForm.php b/system/src/Grav/Framework/Flex/FlexForm.php index ff5f350b6..330e556d5 100644 --- a/system/src/Grav/Framework/Flex/FlexForm.php +++ b/system/src/Grav/Framework/Flex/FlexForm.php @@ -51,7 +51,13 @@ class FlexForm implements FlexFormInterface $this->name = $name; $this->form = $form; - $uniqueId = $object->exists() ? $object->getStorageKey() : "{$object->getFlexType()}:new"; + if ($object->exists()) { + $uniqueId = $object->getStorageKey(); + } elseif ($object->hasKey()) { + $uniqueId = "{$object->getKey()}:new"; + } else { + $uniqueId = "{$object->getFlexType()}:new"; + } $this->setObject($object); $this->setId($this->getName()); $this->setUniqueId(md5($uniqueId)); @@ -127,6 +133,32 @@ class FlexForm implements FlexFormInterface return $this->object->getFlexType(); } + /** + * Get form flash object. + * + * @return FlexFormFlash + */ + public function getFlash() + { + if (null === $this->flash) { + $grav = Grav::instance(); + $config = [ + 'session_id' => $this->getSessionId(), + 'unique_id' => $this->getUniqueId(), + 'form_name' => $this->getName(), + 'folder' => $this->getFlashFolder(), + 'object' => $this->getObject() + ]; + + $this->flash = new FlexFormFlash($config); + $this->flash + ->setUrl($grav['uri']->url) + ->setUser($grav['user'] ?? null); + } + + return $this->flash; + } + /** * @return FlexObjectInterface */ diff --git a/system/src/Grav/Framework/Flex/FlexFormFlash.php b/system/src/Grav/Framework/Flex/FlexFormFlash.php new file mode 100644 index 000000000..c0d280104 --- /dev/null +++ b/system/src/Grav/Framework/Flex/FlexFormFlash.php @@ -0,0 +1,68 @@ +object = $object; + } + + public function getObject(): FlexObjectInterface + { + return $this->object; + } + + public function jsonSerialize(): array + { + $object = $this->getObject(); + + $serialized = parent::jsonSerialize(); + if ($object) { + $serialized['object'] = [ + 'type' => $object->getFlexType(), + 'key' => $object->hasKey() ? $object->getKey() : null, + 'storage_key' => $object->exists() ? $object->getStorageKey() : null, + 'timestamp' => $object->getTimestamp(), + 'serialized' => $object->jsonSerialize() + ]; + } + + return $serialized; + } + + protected function init(?array $data, array $config): void + { + parent::init($data, $config); + + $object = $config['object'] ?? null; + + if ($object) { + $this->setObject($object); + /* + $serialized = $data['object'] ?? null; + if ($serialized && !$object->exists()) { + $fields = $data['object']['serialized'] ?? []; + + $object->update($fields); + } + */ + } + } +}