Change when form builder tree structured is validated
authorMatthias Schmidt <gravatronics@live.com>
Sun, 24 Feb 2019 16:58:26 +0000 (17:58 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 24 Feb 2019 16:58:26 +0000 (17:58 +0100)
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

wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php
wcfsetup/install/files/lib/system/form/builder/IFormParentNode.class.php
wcfsetup/install/files/lib/system/form/builder/TFormParentNode.class.php

index 5bdf588c4c5c9e7066191e9638679dd1c4f2bcbd..d7126b2d5f07e6651e4b43844c45b02b05eb4703 100644 (file)
@@ -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)) {
index c17baa945d2e8d8967054b352be59bfa685d8506..38f49220e4d4f32c98d6a90fae122aaf2fbed26f 100644 (file)
@@ -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);
 }
index 96f41dd235ea6a712f0ef21743f99a5ea4cf74ec..da77389d787c2d1cb8fe29eadb2e5c364e2eafe9 100644 (file)
@@ -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
        }
 }