From ea94fb534e526e053dde1b741df33051a592d359 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Thu, 1 Jul 2021 10:06:15 +0200 Subject: [PATCH] Add `ColorFormField` --- .../templates/__colorFormField.tpl | 31 ++++++++ .../files/acp/templates/__colorFormField.tpl | 31 ++++++++ .../builder/field/ColorFormField.class.php | 70 +++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 com.woltlab.wcf/templates/__colorFormField.tpl create mode 100644 wcfsetup/install/files/acp/templates/__colorFormField.tpl create mode 100644 wcfsetup/install/files/lib/system/form/builder/field/ColorFormField.class.php 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' + )); + } + } + } +} -- 2.20.1