From 30231933c1785677bd0197350ee02f1d71486d5c Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sat, 10 Feb 2018 20:32:37 +0100 Subject: [PATCH] Add possibility to require label for form fields See #2509 --- .../lib/system/form/builder/IFormElement.class.php | 7 +++++++ .../lib/system/form/builder/TFormElement.class.php | 10 ++++++++++ .../form/builder/field/AbstractFormField.class.php | 4 ++++ .../form/builder/field/BooleanFormField.class.php | 7 +++++++ 4 files changed, 28 insertions(+) diff --git a/wcfsetup/install/files/lib/system/form/builder/IFormElement.class.php b/wcfsetup/install/files/lib/system/form/builder/IFormElement.class.php index 7d97f3bdef..9b84e4e654 100644 --- a/wcfsetup/install/files/lib/system/form/builder/IFormElement.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/IFormElement.class.php @@ -51,4 +51,11 @@ interface IFormElement extends IFormNode { * @throws \InvalidArgumentException if the given label is invalid */ public function label(string $languageItem = null, array $variables = []): IFormElement; + + /** + * Returns `true` if this element requires a label to be set. + * + * @return bool + */ + public function requiresLabel(): bool; } diff --git a/wcfsetup/install/files/lib/system/form/builder/TFormElement.class.php b/wcfsetup/install/files/lib/system/form/builder/TFormElement.class.php index 0b983835f7..9a099ff8b8 100644 --- a/wcfsetup/install/files/lib/system/form/builder/TFormElement.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/TFormElement.class.php @@ -104,4 +104,14 @@ trait TFormElement { return $this; } + + /** + * Returns `true` if this element requires a label to be set. + * + * @return bool + */ + public function requiresLabel(): bool { + // by default, form elements do not require a label + return false; + } } diff --git a/wcfsetup/install/files/lib/system/form/builder/field/AbstractFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/AbstractFormField.class.php index 0aaedbdd2b..b652d2c100 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/AbstractFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/AbstractFormField.class.php @@ -107,6 +107,10 @@ abstract class AbstractFormField implements IFormField { throw new \LogicException("\$templateName property has not been set."); } + if ($this->requiresLabel() && $this->getLabel() === null) { + throw new \UnexpectedValueException("Form field '{$this->getPrefixedId()}' requires a label."); + } + return WCF::getTPL()->fetch( $this->templateName, 'wcf', diff --git a/wcfsetup/install/files/lib/system/form/builder/field/BooleanFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/BooleanFormField.class.php index a244a9454f..3addaf84ad 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/BooleanFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/BooleanFormField.class.php @@ -36,6 +36,13 @@ class BooleanFormField extends AbstractFormField { return $this; } + /** + * @inheritDoc + */ + public function requiresLabel(): bool { + return true; + } + /** * @inheritDoc */ -- 2.20.1