Read and validate `customClassName`
authorCyperghost <olaf_schmitz_1@t-online.de>
Mon, 18 Nov 2024 10:42:52 +0000 (11:42 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Mon, 18 Nov 2024 10:42:52 +0000 (11:42 +0100)
com.woltlab.wcf/templates/shared_badgeColorFormField.tpl
wcfsetup/install/files/lib/system/form/builder/field/BadgeColorFormField.class.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index 006f92bae5101306c2a000930567a5f800fcde96..7b5680b85bc7958ec37477fb17d854013d0a5296 100644 (file)
@@ -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'}
                                        <span class="labelSelection__span">
-                                               <input type="text" id="{$field->getPrefixedId()}Custom" name="{$field->getPrefixedId()}customCssClassName" value="{$field->getCustomClassName()}" class="long labelSelection__custom__input">
+                                               <input type="text" id="{$field->getPrefixedId()}Custom" {*
+                                                       *}name="{$field->getPrefixedId()}customCssClassName" {*
+                                                   *}value="{$field->getCustomClassName()}" {*
+                                                       *}class="long labelSelection__custom__input" {*
+                                                       *}{if $field->getPattern() !== null} pattern="{$field->getPattern()}"{/if}{*
+                                               *}>
                                        </span>
                                {else}
                                        <span class="labelSelection__span badge label{if $color != 'none'} {$color}{/if}">{$field->getDefaultLabelText()}</span>
index e9607b02ca55cc2d90fdd9e9ce91cb8ae597f3b1..7c2dd684d8ff8cef0e90bc73d58b0845d2dc902f 100644 (file)
@@ -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;
index 62793c611b2d0ef447b54b5fac2ac1de56bb82bb..103a7e936254cc4c8dabdeb94ef26fb8389ade2f 100644 (file)
@@ -4054,6 +4054,7 @@ Dateianhänge:
                <item name="wcf.global.form.required"><![CDATA[Benötigte Angaben]]></item>
                <item name="wcf.global.form.password.button.hide"><![CDATA[Verbergen]]></item>
                <item name="wcf.global.form.password.button.show"><![CDATA[Anzeigen]]></item>
+               <item name="wcf.global.form.error.invalidCssClassName"><![CDATA[Der eingegebene Wert ist keine gültige CSS-Klasse.]]></item>
        </category>
        <category name="wcf.form">
                <item name="wcf.form.field.className"><![CDATA[Klasse]]></item>
index e213f89ae24f3e8424d9473f5173c7328ff185ec..61b77dfd5e8ed8cb0b37e0f796c1515f8bdd1241 100644 (file)
@@ -4000,6 +4000,7 @@ Attachments:
                <item name="wcf.global.form.required"><![CDATA[Required fields]]></item>
                <item name="wcf.global.form.password.button.hide"><![CDATA[Hide]]></item>
                <item name="wcf.global.form.password.button.show"><![CDATA[Show]]></item>
+               <item name="wcf.global.form.error.invalidCssClassName"><![CDATA[The value entered is not a valid CSS class.]]></item>
        </category>
        <category name="wcf.form">
                <item name="wcf.form.field.className"><![CDATA[Class]]></item>