Add flag to signal that form interacts via AJAX requests
authorMatthias Schmidt <gravatronics@live.com>
Sun, 24 Feb 2019 16:14:21 +0000 (17:14 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 24 Feb 2019 16:15:58 +0000 (17:15 +0100)
See #2509

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

index a9db94d53bf6eb62bc27eb60d417d9cbca17aa15..960db3b86d402dc9fea133c75e8b94a8ebcf933c 100644 (file)
@@ -30,19 +30,6 @@ class DialogFormDocument extends FormDocument {
                return $this;
        }
        
-       /**
-        * @inheritDoc
-        */
-       public function getAction() {
-               // do not throw exception if no action has been set as a dialog
-               // form does not require an action to be set
-               if ($this->action === null) {
-                       $this->action = '';
-               }
-               
-               return $this->action;
-       }
-       
        /**
         * @inheritDoc
         */
index c78f2243c76961f39ae249965f8d566bc325e678..5bdf588c4c5c9e7066191e9638679dd1c4f2bcbd 100644 (file)
@@ -31,6 +31,13 @@ class FormDocument implements IFormDocument {
         */
        protected $action;
        
+       /**
+        * `true` if form is requested via an AJAX request or processes data via an AJAX request
+        * and `false` otherwise
+        * @var boolean
+        */
+       protected $ajax;
+       
        /**
         * data handler for this document
         * @var IFormDataHandler
@@ -90,6 +97,15 @@ class FormDocument implements IFormDocument {
                return $this;
        }
        
+       /**
+        * @inheritDoc
+        */
+       public function ajax($ajax = true) {
+               $this->ajax = $ajax;
+               
+               return $this;
+       }
+       
        /**
         * @inheritDoc
         */
@@ -143,7 +159,7 @@ class FormDocument implements IFormDocument {
         * @inheritDoc
         */
        public function getAction() {
-               if ($this->action === null) {
+               if ($this->action === null && !$this->isAjax()) {
                        throw new \BadMethodCallException("Action has not been set.");
                }
                
@@ -268,6 +284,13 @@ class FormDocument implements IFormDocument {
                return !empty($requestData);
        }
        
+       /**
+        * @inheritDoc
+        */
+       public function isAjax() {
+               return $this->ajax;
+       }
+       
        /**
         * @inheritDoc
         */
index bb82d1e8c7b9e6150e448a09605d7a6eea0a4eaf..5f37e7ab57df092317949cd91b36a809ad5b5304 100644 (file)
@@ -35,6 +35,15 @@ interface IFormDocument extends IFormParentNode {
         */
        public function action($action);
        
+       /**
+        * Sets whether this form is requested via an AJAX request or processes data via an AJAX
+        * request and returns his document.
+        * 
+        * @param       boolean         $ajax
+        * @return      static          this document
+        */
+       public function ajax($ajax = true);
+       
        /**
         * Is called once after all nodes have been added to this document.
         * 
@@ -64,7 +73,7 @@ interface IFormDocument extends IFormParentNode {
         * 
         * @return      string                          form action
         * 
-        * @throws      \BadMethodCallException         if no action has been set
+        * @throws      \BadMethodCallException         if no action has been set and `isAjax()` is `false`
         */
        public function getAction();
        
@@ -149,6 +158,16 @@ interface IFormDocument extends IFormParentNode {
         */
        public function hasRequestData($index = null);
        
+       /**
+        * Returns `true` if this form is requested via an AJAX request or processes data via an
+        * AJAX request and `false` otherwise.
+        * 
+        * By default, this method returns `false`.
+        * 
+        * @return      boolean
+        */
+       public function isAjax();
+       
        /**
         * Loads the field values from the given object and returns this document.
         *