*}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>
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.
'none', /* not a real value */
'custom', /* not a real value */
];
+
/**
* @inheritDoc
*/
protected $templateName = 'shared_badgeColorFormField';
+
protected ?string $textReferenceNodeId;
protected string $defaultLabelText;
protected string $customClassName = '';
->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;
return $this->textReferenceNodeId;
}
+ public function hasCustomClassName(): bool
+ {
+ return $this->value === 'custom';
+ }
+
public function getCustomClassName(): string
{
return $this->customClassName;
<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>
<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>