From 1be4eb3a5779c73f5e34b57775fbba19faccff10 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Fri, 23 Aug 2019 11:30:11 +0200 Subject: [PATCH] Add email form field See #3053 --- .../templates/__emailFormField.tpl | 20 ++++ syncTemplates.json | 1 + .../files/acp/templates/__emailFormField.tpl | 20 ++++ .../builder/field/EmailFormField.class.php | 103 ++++++++++++++++++ .../field/TMaximumLengthFormField.class.php | 9 +- wcfsetup/install/lang/de.xml | 3 + wcfsetup/install/lang/en.xml | 3 + 7 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 com.woltlab.wcf/templates/__emailFormField.tpl create mode 100644 wcfsetup/install/files/acp/templates/__emailFormField.tpl create mode 100644 wcfsetup/install/files/lib/system/form/builder/field/EmailFormField.class.php diff --git a/com.woltlab.wcf/templates/__emailFormField.tpl b/com.woltlab.wcf/templates/__emailFormField.tpl new file mode 100644 index 0000000000..8b22d9b50f --- /dev/null +++ b/com.woltlab.wcf/templates/__emailFormField.tpl @@ -0,0 +1,20 @@ +{include file='__formFieldHeader'} + +isAutofocused()} autofocus{/if}{* + *}{if $field->isRequired()} required{/if}{* + *}{if $field->isImmutable()} disabled{/if}{* + *}{if $field->getMaximumLength() !== null} maxlength="{$field->getMaximumLength()}"{/if}{* + *}{if $field->getPlaceholder() !== null} placeholder="{$field->getPlaceholder()}"{/if}{* + *}{if $field->getDocument()->isAjax()} data-dialog-submit-on-enter="true"{/if}{* +*}> + +{if $field->isI18n()} + {include file='multipleLanguageInputJavascript'} +{/if} + +{include file='__formFieldFooter'} diff --git a/syncTemplates.json b/syncTemplates.json index 3387a2ce26..d1f698bfbe 100644 --- a/syncTemplates.json +++ b/syncTemplates.json @@ -8,6 +8,7 @@ "__booleanFormField", "__contentLanguageFormField", "__dateFormField", + "__emailFormField", "__emptyFormFieldDependency", "__form", "__formButton", diff --git a/wcfsetup/install/files/acp/templates/__emailFormField.tpl b/wcfsetup/install/files/acp/templates/__emailFormField.tpl new file mode 100644 index 0000000000..8b22d9b50f --- /dev/null +++ b/wcfsetup/install/files/acp/templates/__emailFormField.tpl @@ -0,0 +1,20 @@ +{include file='__formFieldHeader'} + +isAutofocused()} autofocus{/if}{* + *}{if $field->isRequired()} required{/if}{* + *}{if $field->isImmutable()} disabled{/if}{* + *}{if $field->getMaximumLength() !== null} maxlength="{$field->getMaximumLength()}"{/if}{* + *}{if $field->getPlaceholder() !== null} placeholder="{$field->getPlaceholder()}"{/if}{* + *}{if $field->getDocument()->isAjax()} data-dialog-submit-on-enter="true"{/if}{* +*}> + +{if $field->isI18n()} + {include file='multipleLanguageInputJavascript'} +{/if} + +{include file='__formFieldFooter'} diff --git a/wcfsetup/install/files/lib/system/form/builder/field/EmailFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/EmailFormField.class.php new file mode 100644 index 0000000000..204ee60ea2 --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/EmailFormField.class.php @@ -0,0 +1,103 @@ + + * @package WoltLabSuite\Core\System\Form\Builder\Field + * @since 5.2 + */ +class EmailFormField extends AbstractFormField implements IAutoFocusFormField, II18nFormField, IImmutableFormField, IMaximumLengthFormField, IPlaceholderFormField { + use TAutoFocusFormField; + use TImmutableFormField; + use TI18nFormField { + validate as protected i18nValidate; + } + use TMaximumLengthFormField; + use TPlaceholderFormField; + + /** + * @inheritDoc + */ + protected $javaScriptDataHandlerModule = 'WoltLabSuite/Core/Form/Builder/Field/Value'; + + /** + * @inheritDoc + */ + protected $templateName = '__emailFormField'; + + /** + * @inheritDoc + */ + public function __construct() { + $this->label('wcf.form.field.email'); + } + + /** + * @inheritDoc + */ + public function validate() { + if ($this->isI18n()) { + $this->i18nValidate(); + + if (empty($this->getValidationErrors())) { + $value = $this->getValue(); + if ($this->hasPlainValue()) { + $this->validateEmail($value); + } + else { + foreach ($value as $languageID => $languageValue) { + $this->validateEmail($languageValue, LanguageFactory::getInstance()->getLanguage($languageID)); + } + } + } + } + else { + if ($this->isRequired() && ($this->getValue() === null || $this->getValue() === '')) { + $this->addValidationError(new FormFieldValidationError('empty')); + } + else { + $this->validateEmail($this->getValue()); + } + } + + parent::validate(); + } + + /** + * Validates the given email address in the given language. + * + * @param string $email validated email address + * @param null|Language $language language of validated email address or `null` for monolingual email address + */ + protected function validateEmail($email, Language $language = null) { + if ($email === null || $email === '') { + return; + } + + if (!UserUtil::isValidEmail($email)) { + $this->addValidationError(new FormFieldValidationError( + 'invalidEmail', + 'wcf.form.field.email.error.invalidEmail', + ['language' => $language] + )); + } + else { + $this->validateMaximumLength( + $email, + $language, + 'wcf.form.field.email.error.maximumLength' + ); + } + } +} diff --git a/wcfsetup/install/files/lib/system/form/builder/field/TMaximumLengthFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/TMaximumLengthFormField.class.php index 44ab4ec0d8..b22cf52adb 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/TMaximumLengthFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/TMaximumLengthFormField.class.php @@ -64,14 +64,15 @@ trait TMaximumLengthFormField { /** * Validates the maximum length of the given text. * - * @param string $text validated text - * @param null|Language $language language of the validated text + * @param string $text validated text + * @param null|Language $language language of the validated text + * @param string $errorLanguageItem */ - public function validateMaximumLength($text, Language $language = null) { + public function validateMaximumLength($text, Language $language = null, $errorLanguageItem = 'wcf.form.field.text.error.maximumLength') { if ($this->getMaximumLength() !== null && mb_strlen($text) > $this->getMaximumLength()) { $this->addValidationError(new FormFieldValidationError( 'maximumLength', - 'wcf.form.field.text.error.maximumLength', + $errorLanguageItem, [ 'language' => $language, 'length' => mb_strlen($text), diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 4ceef302c9..5040ec8453 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -3919,6 +3919,9 @@ Dateianhänge: + + + diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 52a8cb1861..2dac401c24 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -3865,6 +3865,9 @@ Attachments: + + + -- 2.20.1