* Fixed FlexForm to allow multiple form instances with non-existing objects

This commit is contained in:
Matias Griese
2019-07-11 15:24:35 +03:00
parent 69d6b52a0e
commit 53216631a6
3 changed files with 102 additions and 1 deletions

View File

@@ -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

View File

@@ -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
*/

View File

@@ -0,0 +1,68 @@
<?php
/**
* @package Grav\Common\Flex
*
* @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Framework\Flex;
use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
use Grav\Framework\Form\FormFlash;
class FlexFormFlash extends FormFlash
{
/**
* @var FlexObjectInterface
*/
protected $object;
public function setObject(FlexObjectInterface $object)
{
$this->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);
}
*/
}
}
}