From: Matthias Schmidt Date: Thu, 1 Jul 2021 08:06:15 +0000 (+0200) Subject: Add `ColorFormField` X-Git-Tag: 5.5.0_Alpha_1~583^2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=ea94fb534e526e053dde1b741df33051a592d359;p=GitHub%2FWoltLab%2FWCF.git Add `ColorFormField` --- diff --git a/com.woltlab.wcf/templates/__colorFormField.tpl b/com.woltlab.wcf/templates/__colorFormField.tpl new file mode 100644 index 0000000000..f0ba0159c5 --- /dev/null +++ b/com.woltlab.wcf/templates/__colorFormField.tpl @@ -0,0 +1,31 @@ +{if $field->isImmutable()} + + getValue()} style="background-color: {$field->getValue()}"{/if}> + +{else} + + getValue()} style="background-color: {$field->getValue()}"{/if}> + + + + +{/if} diff --git a/wcfsetup/install/files/acp/templates/__colorFormField.tpl b/wcfsetup/install/files/acp/templates/__colorFormField.tpl new file mode 100644 index 0000000000..f0ba0159c5 --- /dev/null +++ b/wcfsetup/install/files/acp/templates/__colorFormField.tpl @@ -0,0 +1,31 @@ +{if $field->isImmutable()} + + getValue()} style="background-color: {$field->getValue()}"{/if}> + +{else} + + getValue()} style="background-color: {$field->getValue()}"{/if}> + + + + +{/if} diff --git a/wcfsetup/install/files/lib/system/form/builder/field/ColorFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/ColorFormField.class.php new file mode 100644 index 0000000000..80eb310604 --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/ColorFormField.class.php @@ -0,0 +1,70 @@ + + * @package WoltLabSuite\Core\System\Form\Builder\Field + * @since 5.5 + */ +class ColorFormField extends AbstractFormField implements IImmutableFormField +{ + use TImmutableFormField; + + /** + * @inheritDoc + */ + protected $javaScriptDataHandlerModule = 'WoltLabSuite/Core/Form/Builder/Field/Value'; + + /** + * @inheritDoc + */ + protected $templateName = '__colorFormField'; + + /** + * @inheritDoc + */ + public function readValue() + { + if ( + $this->getDocument()->hasRequestData($this->getPrefixedId()) + && \is_string($this->getDocument()->getRequestData($this->getPrefixedId())) + ) { + $this->value = $this->getDocument()->getRequestData($this->getPrefixedId()); + + if ($this->value === '') { + $this->value = null; + } + } + + return $this; + } + + /** + * @inheritDoc + */ + public function validate() + { + parent::validate(); + + if ($this->getValue() === null) { + if ($this->isRequired()) { + $this->addValidationError(new FormFieldValidationError('empty')); + } + } else { + // @see StyleAddForm::readFormParameters() + if (!\preg_match('~rgba\(\d{1,3}, ?\d{1,3}, ?\d{1,3}, ?(1|1\.00?|0|0?\.[0-9]{1,2})\)~', $this->getValue())) { + $this->addValidationError(new FormFieldValidationError( + 'invalid', + 'wcf.style.colorPicker.error.invalidColor' + )); + } + } + } +}