Throw exception when getting form data before reading values
authorMatthias Schmidt <gravatronics@live.com>
Thu, 11 Apr 2019 16:22:45 +0000 (18:22 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Thu, 11 Apr 2019 16:22:45 +0000 (18:22 +0200)
Close #2889

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

index 9116846ef5879fbf6c2e749c229eaa337cbe29f3..302b00248ce110834c2aafab8502c131dd97ea56 100644 (file)
@@ -63,6 +63,12 @@ class FormDocument implements IFormDocument {
         */
        protected $dataHandler;
        
+       /**
+        * indicates if the form data has been read via `readData()`
+        * @var boolean
+        */
+       protected $didReadValues = false;
+       
        /**
         * encoding type of this form
         * @var null|
@@ -249,6 +255,13 @@ class FormDocument implements IFormDocument {
                );
        }
        
+       /**
+        * @inheritDoc
+        */
+       public function didReadValues() {
+               return $this->didReadValues;
+       }
+       
        /**
         * @inheritDoc
         */
@@ -321,6 +334,10 @@ class FormDocument implements IFormDocument {
         * @inheritDoc
         */
        public function getData() {
+               if (!$this->didReadValues()) {
+                       throw new \BadMethodCallException("Getting data is only possible after calling 'readValues()'.");
+               }
+               
                return $this->getDataHandler()->getData($this);
        }
        
@@ -573,6 +590,8 @@ class FormDocument implements IFormDocument {
                        $this->requestData = $_POST;
                }
                
+               $this->didReadValues = true;
+               
                return $this->traitReadValues();
        }
        
index 0a9df5b0c10e3042c66e29051e5516fbb305ef29..df0cb38ba93d742c0dcef68109ef9b080bdcc459 100644 (file)
@@ -76,6 +76,13 @@ interface IFormDocument extends IFormParentNode {
         */
        public function build();
        
+       /**
+        * Returns `true` if the form data has been read via `readData()` and `false` otherwise.
+        * 
+        * @return      boolean
+        */
+       public function didReadValues();
+       
        /**
         * Sets the error message of this form using the given language item and returns this
         * document. If `null` is passed, the error message is unset.
@@ -133,6 +140,8 @@ interface IFormDocument extends IFormParentNode {
         * of a database object action.
         * 
         * @return      array           data passed to database object action
+        * 
+        * @throws      \BadMethodCallException         if the method is called before `readValues()` is called
         */
        public function getData();