From a6cecaa156509e75ecfc26e51a099223013bb9ae Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Tue, 8 Dec 2020 18:03:41 +0100 Subject: [PATCH] Make marking required fields optional with form builder (#3764) Close #3758 --- com.woltlab.wcf/templates/__formField.tpl | 2 +- .../files/acp/templates/__formField.tpl | 2 +- .../form/builder/FormDocument.class.php | 27 +++++++++++++++++++ .../form/builder/IFormDocument.class.php | 16 +++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/com.woltlab.wcf/templates/__formField.tpl b/com.woltlab.wcf/templates/__formField.tpl index d59c467b7d..e78e5d179b 100644 --- a/com.woltlab.wcf/templates/__formField.tpl +++ b/com.woltlab.wcf/templates/__formField.tpl @@ -1,5 +1,5 @@
getClasses()|empty} class="{implode from=$field->getClasses() item='class' glue=' '}{$class}{/implode}"{/if}{foreach from=$field->getAttributes() key='attributeName' item='attributeValue'} {$attributeName}="{$attributeValue}"{/foreach}{if !$field->checkDependencies()} style="display: none;"{/if}> -
{if $field->getLabel() !== null}{if $field->isRequired()} *{/if}{/if}
+
{if $field->getLabel() !== null}{if $field->isRequired() && $form->marksRequiredFields()} *{/if}{/if}
{@$field->getFieldHtml()} diff --git a/wcfsetup/install/files/acp/templates/__formField.tpl b/wcfsetup/install/files/acp/templates/__formField.tpl index d59c467b7d..e78e5d179b 100644 --- a/wcfsetup/install/files/acp/templates/__formField.tpl +++ b/wcfsetup/install/files/acp/templates/__formField.tpl @@ -1,5 +1,5 @@
getClasses()|empty} class="{implode from=$field->getClasses() item='class' glue=' '}{$class}{/implode}"{/if}{foreach from=$field->getAttributes() key='attributeName' item='attributeValue'} {$attributeName}="{$attributeValue}"{/foreach}{if !$field->checkDependencies()} style="display: none;"{/if}> -
{if $field->getLabel() !== null}{if $field->isRequired()} *{/if}{/if}
+
{if $field->getLabel() !== null}{if $field->isRequired() && $form->marksRequiredFields()} *{/if}{/if}
{@$field->getFieldHtml()} 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 88308ebbe7..97a7dd7806 100644 --- a/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php @@ -99,6 +99,11 @@ class FormDocument implements IFormDocument { */ protected $formMode; + /** + * @var bool + */ + protected $markRequiredFields = true; + /** * `method` property of the HTML `form` element * @var string @@ -527,11 +532,33 @@ class FormDocument implements IFormDocument { return $this->invalid; } + /** + * @inheritDoc + * @since 5.4 + */ + public function markRequiredFields(bool $markRequiredFields = true): self { + $this->markRequiredFields = $markRequiredFields; + + return $this; + } + + /** + * @inheritDoc + * @since 5.4 + */ + public function marksRequiredFields(): bool { + return $this->markRequiredFields; + } + /** * @inheritDoc * @since 5.3 */ public function needsRequiredFieldsInfo() { + if (!$this->marksRequiredFields()) { + return false; + } + /** @var IFormNode $node */ foreach ($this->getIterator() as $node) { if ( 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 6009d40b2d..91ee25fbdd 100644 --- a/wcfsetup/install/files/lib/system/form/builder/IFormDocument.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/IFormDocument.class.php @@ -308,6 +308,22 @@ interface IFormDocument extends IFormParentNode { */ public function updatedObject(IStorableObject $object, $loadValues = true); + /** + * Sets whether required fields are marked in the output and returns this document. + * + * @since 5.4 + */ + public function markRequiredFields(bool $markRequiredFields = true): self; + + /** + * Returns `true` if required fields are marked in the output and `false` otherwise. + * + * By default, required fields are marked in the output. + * + * @since 5.4 + */ + public function marksRequiredFields(): bool; + /** * Sets the `method` property of the HTML `form` element and returns this document. * -- 2.20.1