From: Matthias Schmidt Date: Sat, 17 Mar 2018 07:13:12 +0000 (+0100) Subject: Add form mode to form document X-Git-Tag: 5.2.0_Alpha_1~680^2~84 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=8310af4aa04f7a1c7cc29c769eb7258d944b7640;p=GitHub%2FWoltLab%2FWCF.git Add form mode to form document 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 fa4c353838..7385ba9efe 100644 --- a/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php @@ -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; } /** diff --git a/wcfsetup/install/files/lib/system/form/builder/IFormDocument.class.php b/wcfsetup/install/files/lib/system/form/builder/IFormDocument.class.php index 52b4d928d7..4a694e12c8 100644 --- a/wcfsetup/install/files/lib/system/form/builder/IFormDocument.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/IFormDocument.class.php @@ -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