From: Matthias Schmidt Date: Sun, 24 Feb 2019 16:58:26 +0000 (+0100) Subject: Change when form builder tree structured is validated X-Git-Tag: 5.2.0_Alpha_1~261 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=e1f737076a3125b345b94cd0c6ba7407efaa26c9;p=GitHub%2FWoltLab%2FWCF.git Change when form builder tree structured is validated Instead of directly validating children when they are added, they are now validated when the form is built. At this point in time, the complete form tree is established allowing for a greater varity of validations. See #2509 --- diff --git a/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php b/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php index 5bdf588c4c..d7126b2d5f 100644 --- a/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php @@ -127,6 +127,12 @@ class FormDocument implements IFormDocument { } $node->populate(); + + if ($node instanceof IFormParentNode) { + foreach ($node->children() as $child) { + $node->validateChild($child); + } + } } if (!empty($doubleNodeIds)) { diff --git a/wcfsetup/install/files/lib/system/form/builder/IFormParentNode.class.php b/wcfsetup/install/files/lib/system/form/builder/IFormParentNode.class.php index c17baa945d..38f49220e4 100644 --- a/wcfsetup/install/files/lib/system/form/builder/IFormParentNode.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/IFormParentNode.class.php @@ -16,8 +16,6 @@ interface IFormParentNode extends \Countable, IFormNode, \RecursiveIterator { * * @param IFormChildNode $child appended child * @return static this node - * - * @throws \InvalidArgumentException if the given child node cannot be appended */ public function appendChild(IFormChildNode $child); @@ -26,8 +24,6 @@ interface IFormParentNode extends \Countable, IFormNode, \RecursiveIterator { * * @param IFormChildNode[] $children appended children * @return static this node - * - * @throws \InvalidArgumentException if any of the given child nodes is invalid or cannot be appended */ public function appendChildren(array $children); @@ -97,11 +93,11 @@ interface IFormParentNode extends \Countable, IFormNode, \RecursiveIterator { public function readValues(); /** - * Checks if the given node can be added as a child to this node. + * Checks if the given node is a valid child for this node. * * @param IFormChildNode $child validated child node * - * @throws \InvalidArgumentException if given node cannot be added as a child + * @throws \InvalidArgumentException if given node cannot is an invalid child */ public function validateChild(IFormChildNode $child); } diff --git a/wcfsetup/install/files/lib/system/form/builder/TFormParentNode.class.php b/wcfsetup/install/files/lib/system/form/builder/TFormParentNode.class.php index 96f41dd235..da77389d78 100644 --- a/wcfsetup/install/files/lib/system/form/builder/TFormParentNode.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/TFormParentNode.class.php @@ -32,12 +32,8 @@ trait TFormParentNode { * * @param IFormChildNode $child appended child * @return static this node - * - * @throws \InvalidArgumentException if the given child node cannot be appended */ public function appendChild(IFormChildNode $child) { - $this->validateChild($child); - $this->children[] = $child; $child->parent($this); @@ -50,8 +46,6 @@ trait TFormParentNode { * * @param IFormChildNode[] $children appended children * @return static this node - * - * @throws \InvalidArgumentException if any of the given child nodes is invalid or cannot be appended */ public function appendChildren(array $children) { foreach ($children as $child) { @@ -336,19 +330,6 @@ trait TFormParentNode { * @throws \InvalidArgumentException if given node cannot be added as a child */ public function validateChild(IFormChildNode $child) { - // check if a node with same id as the given node already exists - if ($this->contains($child->getId())) { - throw new \InvalidArgumentException("Cannot append node '{$child->getId()}' to node '{$this->getId()}' because a node with id '{$child->getId()}' already exists."); - } - - // check all child nodes of the given node for duplicate node ids - if ($child instanceof IFormParentNode) { - /** @var IFormNode $thisChild */ - foreach ($child->getIterator() as $grandChild) { - if ($grandChild instanceof IFormParentNode && $this->contains($grandChild->getId())) { - throw new \InvalidArgumentException("Cannot append node '{$child->getId()}' to node '{$this->getId()}' because '{$child->getId()}' contains a node with id '{$grandChild->getId()}' that is already used by another node."); - } - } - } + // does nothing } }