Add form mode to form document
authorMatthias Schmidt <gravatronics@live.com>
Sat, 17 Mar 2018 07:13:12 +0000 (08:13 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 17 Mar 2018 07:13:12 +0000 (08:13 +0100)
See #2509

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

index fa4c3538387cfe089abb964ee6a9a0630473110f..7385ba9efe84fe5cd5845988a7c6255861e14c24 100644 (file)
@@ -26,6 +26,12 @@ class FormDocument implements IFormDocument {
         */
        protected $__action;
        
+       /**
+        * form mode (see `self::FORM_MODE_*` constants)
+        * @var null|string
+        */
+       protected $__formMode;
+       
        /**
         * `method` property of the HTML `form` element
         * @var string
@@ -98,6 +104,23 @@ class FormDocument implements IFormDocument {
                return $this;
        }
        
+       /**
+        * @inheritDoc
+        */
+       public function formMode(string $formMode): IFormDocument {
+               if ($this->__formMode !== null) {
+                       throw new \BadMethodCallException("Form mode has already been set");
+               }
+               
+               if ($formMode !== self::FORM_MODE_CREATE && $formMode !== self::FORM_MODE_UPDATE) {
+                       throw new \InvalidArgumentException("Unknown form mode '{$formMode}' given.");
+               }
+               
+               $this->__formMode = $formMode;
+               
+               return $this;
+       }
+       
        /**
         * @inheritDoc
         */
@@ -155,6 +178,17 @@ class FormDocument implements IFormDocument {
                return $this->enctype;
        }
        
+       /**
+        * @inheritDoc
+        */
+       public function getFormMode(): string {
+               if ($this->__formMode === null) {
+                       $this->__formMode = self::FORM_MODE_CREATE;
+               }
+               
+               return $this->__formMode;
+       }
+       
        /**
         * @inheritDoc
         */
@@ -174,26 +208,28 @@ class FormDocument implements IFormDocument {
        /**
         * @inheritDoc
         */
-       public function loadValuesFromObject(IStorableObject $object): IFormDocument {
-               /** @var IFormNode $node */
-               foreach ($this->getIterator() as $node) {
-                       if ($node instanceof IFormField && $node->isAvailable()) {
-                               $node->loadValueFromObject($object);
-                       }
+       public function getPrefix(): string {
+               if ($this->__prefix === null) {
+                       return '';
                }
                
-               return $this;
+               return $this->__prefix . '_';
        }
        
        /**
         * @inheritDoc
         */
-       public function getPrefix(): string {
-               if ($this->__prefix === null) {
-                       return '';
+       public function loadValuesFromObject(IStorableObject $object): IFormDocument {
+               $this->formMode(self::FORM_MODE_UPDATE);
+               
+               /** @var IFormNode $node */
+               foreach ($this->getIterator() as $node) {
+                       if ($node instanceof IFormField && $node->isAvailable()) {
+                               $node->loadValueFromObject($object);
+                       }
                }
                
-               return $this->__prefix . '_';
+               return $this;
        }
        
        /**
index 52b4d928d708fb6be36775111c28897a4d6b858f..4a694e12c817e78914a92eb53d3258f90d12ed44 100644 (file)
@@ -13,6 +13,18 @@ use wcf\data\IStorableObject;
  * @since      3.2
  */
 interface IFormDocument extends IFormParentNode {
+       /**
+        * represents the form mode for creating a new object
+        * @var string
+        */
+       const FORM_MODE_CREATE = 'create';
+       
+       /**
+        * represents the form mode for updating a new object
+        * @var string
+        */
+       const FORM_MODE_UPDATE = 'update';
+       
        /**
         * Sets the `action` property of the HTML `form` element and returns this document. 
         * 
@@ -36,6 +48,17 @@ interface IFormDocument extends IFormParentNode {
         */
        public function build(): IFormDocument;
        
+       /**
+        * Sets the form mode (see `self::FORM_MODE_*` constants).
+        * 
+        * @param       string          $formMode       form mode
+        * @return      static                          this document
+        * 
+        * @throws      \BadMethodCallException         if the form mode has already been set
+        * @throws      \InvalidArgumentException       if the given form mode is invalid
+        */
+       public function formMode(string $formMode): IFormDocument;
+       
        /**
         * Returns the `action` property of the HTML `form` element.
         * 
@@ -74,6 +97,17 @@ interface IFormDocument extends IFormParentNode {
         */
        public function getEnctype();
        
+       /**
+        * Returns the form mode (see `self::FORM_MODE_*` constants).
+        *
+        * The form mode can help validators to determine whether a new object
+        * is added or an existing object is edited. If no form mode has been
+        * explicitly set, `self::FORM_MODE_CREATE` is set and returned.
+        * 
+        * @return      string          form mode
+        */
+       public function getFormMode(): string;
+       
        /**
         * Returns the `method` property of the HTML `form` element. If no method
         * has been set, `post` is returned.
@@ -96,6 +130,7 @@ interface IFormDocument extends IFormParentNode {
         * Loads the field values from the given object and returns this document.
         * 
         * Per default, for each field, `IFormField::loadValueFromObject()` is called.
+        * This method automatically sets the form mode to `self::FORM_MODE_UPDATE`.
         * 
         * @param       IStorableObject         $object         object used to load field values
         * @return      static                                  this document