From e5da06b6db8f3d83206b48f79d7e7b1f08997e02 Mon Sep 17 00:00:00 2001 From: Cyperghost Date: Mon, 18 Nov 2024 11:42:52 +0100 Subject: [PATCH] Read and validate `customClassName` --- .../templates/shared_badgeColorFormField.tpl | 9 +++- .../field/BadgeColorFormField.class.php | 53 +++++++++++++++++++ wcfsetup/install/lang/de.xml | 1 + wcfsetup/install/lang/en.xml | 1 + 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/com.woltlab.wcf/templates/shared_badgeColorFormField.tpl b/com.woltlab.wcf/templates/shared_badgeColorFormField.tpl index 006f92bae5..7b5680b85b 100644 --- a/com.woltlab.wcf/templates/shared_badgeColorFormField.tpl +++ b/com.woltlab.wcf/templates/shared_badgeColorFormField.tpl @@ -7,13 +7,18 @@ *}name="{$field->getPrefixedId()}" {* *}value="{$color}"{* *}{if !$field->getFieldClasses()|empty} class="{implode from=$field->getFieldClasses() item=class glue=' '}{$class}{/implode}"{/if}{* - *}{if $field->getValue() !== null && $field->getValue() == $color} checked{/if}{* + *}{if $field->getValue() === $color || ($color === 'custom' && !$field->getCustomClassName()|empty)} checked{/if}{* *}{if $field->isImmutable()} disabled{/if}{* *}{foreach from=$field->getFieldAttributes() key=attributeName item=attributeValue} {$attributeName}="{$attributeValue}"{/foreach}{* *}> {if $color == 'custom'} - + getPattern() !== null} pattern="{$field->getPattern()}"{/if}{* + *}> {else} {$field->getDefaultLabelText()} diff --git a/wcfsetup/install/files/lib/system/form/builder/field/BadgeColorFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/BadgeColorFormField.class.php index e9607b02ca..7c2dd684d8 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/BadgeColorFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/BadgeColorFormField.class.php @@ -2,7 +2,10 @@ namespace wcf\system\form\builder\field; +use wcf\system\form\builder\field\validation\FormFieldValidationError; +use wcf\system\Regex; use wcf\system\WCF; +use wcf\util\StringUtil; /** * Implementation of a badge color form field for selecting a single color or a custom color. @@ -31,10 +34,12 @@ final class BadgeColorFormField extends RadioButtonFormField implements IPattern 'none', /* not a real value */ 'custom', /* not a real value */ ]; + /** * @inheritDoc */ protected $templateName = 'shared_badgeColorFormField'; + protected ?string $textReferenceNodeId; protected string $defaultLabelText; protected string $customClassName = ''; @@ -48,6 +53,49 @@ final class BadgeColorFormField extends RadioButtonFormField implements IPattern ->pattern('^-?[_a-zA-Z]+[_a-zA-Z0-9-]+$'); } + #[\Override] + public function readValue() + { + if ($this->getDocument()->hasRequestData($this->getPrefixedId())) { + $this->value = StringUtil::trim($this->getDocument()->getRequestData($this->getPrefixedId())); + + if ($this->value === 'custom') { + $this->customClassName = StringUtil::trim( + $this->getDocument()->getRequestData($this->getPrefixedId() . 'customCssClassName') + ); + } + } + + return $this; + } + + #[\Override] + public function validate() + { + if ($this->getValue() === 'custom') { + if (!Regex::compile($this->getPattern())->match($this->customClassName)) { + $this->addValidationError( + new FormFieldValidationError( + 'invalid', + 'wcf.global.form.error.invalidCssClassName' + ) + ); + } + } else { + parent::validate(); + } + } + + #[\Override] + public function getSaveValue() + { + if ($this->hasCustomClassName()) { + return $this->getCustomClassName(); + } + + return $this->getValue(); + } + public function defaultLabelText(string $text): self { $this->defaultLabelText = $text; @@ -79,6 +127,11 @@ final class BadgeColorFormField extends RadioButtonFormField implements IPattern return $this->textReferenceNodeId; } + public function hasCustomClassName(): bool + { + return $this->value === 'custom'; + } + public function getCustomClassName(): string { return $this->customClassName; diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 62793c611b..103a7e9362 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -4054,6 +4054,7 @@ Dateianhänge: + diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index e213f89ae2..61b77dfd5e 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -4000,6 +4000,7 @@ Attachments: + -- 2.20.1