Add email form field
authorMatthias Schmidt <gravatronics@live.com>
Fri, 23 Aug 2019 09:30:11 +0000 (11:30 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Fri, 23 Aug 2019 09:30:11 +0000 (11:30 +0200)
See #3053

com.woltlab.wcf/templates/__emailFormField.tpl [new file with mode: 0644]
syncTemplates.json
wcfsetup/install/files/acp/templates/__emailFormField.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/system/form/builder/field/EmailFormField.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/form/builder/field/TMaximumLengthFormField.class.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

diff --git a/com.woltlab.wcf/templates/__emailFormField.tpl b/com.woltlab.wcf/templates/__emailFormField.tpl
new file mode 100644 (file)
index 0000000..8b22d9b
--- /dev/null
@@ -0,0 +1,20 @@
+{include file='__formFieldHeader'}
+
+<input type="email" {*
+       *}id="{@$field->getPrefixedId()}" {*
+       *}name="{@$field->getPrefixedId()}" {*
+       *}value="{if !$field->isI18n() || !$field->hasI18nValues()}{$field->getValue()}{/if}" {*
+       *}class="long"{*
+       *}{if $field->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'}
index 3387a2ce26f4dcf13bd8be145fdfcc25d494ec18..d1f698bfbe4e283df43bade43d474f2e1c050c23 100644 (file)
@@ -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 (file)
index 0000000..8b22d9b
--- /dev/null
@@ -0,0 +1,20 @@
+{include file='__formFieldHeader'}
+
+<input type="email" {*
+       *}id="{@$field->getPrefixedId()}" {*
+       *}name="{@$field->getPrefixedId()}" {*
+       *}value="{if !$field->isI18n() || !$field->hasI18nValues()}{$field->getValue()}{/if}" {*
+       *}class="long"{*
+       *}{if $field->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 (file)
index 0000000..204ee60
--- /dev/null
@@ -0,0 +1,103 @@
+<?php
+namespace wcf\system\form\builder\field;
+use wcf\data\language\Language;
+use wcf\system\exception\UserInputException;
+use wcf\system\form\builder\field\validation\FormFieldValidationError;
+use wcf\system\language\LanguageFactory;
+use wcf\util\UserUtil;
+
+/**
+ * Implementation of a form field for an email address.
+ * 
+ * The default label of fields of this class is `wcf.form.field.email`.
+ * 
+ * @author     Matthias Schmidt
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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'
+                       );
+               }
+       }
+}
index 44ab4ec0d80545ff560d804cae38c179581466fb..b22cf52adb2f56c9bd62ec4eb2fadbc4ae1bcc83 100644 (file)
@@ -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),
index 4ceef302c9dcf34c3fe71ecccde711a7d8b12bc1..5040ec8453dc99c488a42a4d740b32895b4b5737 100644 (file)
@@ -3919,6 +3919,9 @@ Dateianhänge:
                <item name="wcf.form.field.date.error.latestDate"><![CDATA[Der angegebene Wert darf nicht später sein als {$latestDate}.]]></item>
                <item name="wcf.form.field.itemList.error.maximumMultiples"><![CDATA[{if LANGUAGE_USE_INFORMAL_VARIANT}Du darfst{else}Sie dürfen{/if} nicht mehr als {#$maximumCount} {if $maximumCount == 1}Eintrag{else}Einträge{/if} angeben, es wurde{if $count != 1}n{/if} aber {#$count} {if $count == 1}Eintrag{else}Einträge{/if} angegeben.]]></item>
                <item name="wcf.form.field.itemList.error.minimumMultiples"><![CDATA[{if LANGUAGE_USE_INFORMAL_VARIANT}Du musst{else}Sie müssen{/if} zumindest {#$minimumCount} {if $minimumCount == 1}Eintrag{else}Einträge{/if} angeben, es wurde{if $count != 1}n{/if} aber nur {#$count} {if $count == 1}Eintrag{else}Einträge{/if} angegeben.]]></item>
+               <item name="wcf.form.field.email"><![CDATA[E-Mail-Adresse]]></item>
+               <item name="wcf.form.field.email.error.invalidEmail"><![CDATA[Die Eingabe ist keine gültige E-Mail-Adresse.]]></item>
+               <item name="wcf.form.field.email.error.maximumLength"><![CDATA[Die angegebene E-Mail-Adresse{if $language} für die Sprache „{$language}“{/if} umfasst {#$length} Zeichen, sie darf aber nicht länger als {#$maximumLength} Zeichen sein.]]></item>
        </category>
        <category name="wcf.image">
                <item name="wcf.image.coverPhoto"><![CDATA[Titelbild]]></item>
index 52a8cb1861c7fb86a2a56ff55313c410a4cb388f..2dac401c24d312464921ab3d56b2e2233b2dc163 100644 (file)
@@ -3865,6 +3865,9 @@ Attachments:
                <item name="wcf.form.field.date.error.latestDate"><![CDATA[The entered value may not be later than {$latestDate}.]]></item>
                <item name="wcf.form.field.itemList.error.maximumMultiples"><![CDATA[You may not enter more than {#$maximumCount} {if $maximumCount == 1}entry{else}entries{/if} but {#$count} {if $count == 1}entry is{else}entries are{/if} given.]]></item>
                <item name="wcf.form.field.itemList.error.minimumMultiples"><![CDATA[You have to entered at least {#$minimumCount} {if $minimumCount == 1}entry{else}entries{/if} but only {#$count} {if $count == 1}entry is{else}entries are{/if} given.]]></item>
+               <item name="wcf.form.field.email"><![CDATA[Email Address]]></item>
+               <item name="wcf.form.field.email.error.invalidEmail"><![CDATA[The entered text is no valid email address.]]></item>
+               <item name="wcf.form.field.email.error.maximumLength"><![CDATA[The entered email address{if $language|isset} for language “{$language}”{/if} contains {#$length} character{if $length !== 1}s{/if} but it may not be longer than {#$maximumLength} character{if $maximumLength !== 1}s{/if}.]]></item>
        </category>
        <category name="wcf.image">
                <item name="wcf.image.coverPhoto"><![CDATA[Cover Photo]]></item>