Add username form field
authorMatthias Schmidt <gravatronics@live.com>
Tue, 27 Feb 2018 16:15:48 +0000 (17:15 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Tue, 27 Feb 2018 16:15:48 +0000 (17:15 +0100)
See #2509

wcfsetup/install/files/acp/templates/__usernameFormField.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/acp/form/DevtoolsFormBuilderTestForm.class.php
wcfsetup/install/files/lib/system/form/builder/field/UsernameFormField.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/acp/templates/__usernameFormField.tpl b/wcfsetup/install/files/acp/templates/__usernameFormField.tpl
new file mode 100644 (file)
index 0000000..1a2e273
--- /dev/null
@@ -0,0 +1,5 @@
+{include file='__formFieldHeader'}
+
+<input type="text" id="{@$field->getPrefixedId()}" name="{@$field->getPrefixedId()}" value="{$field->getValue()}" class="long"{if $field->isAutofocused()} autofocus{/if}{if $field->isRequired()} required{/if}{if $field->isImmutable()} disabled{/if}{if $field->getMinimumLength() !== null} minlength="{$field->getMinimumLength()}"{/if}{if $field->getMaximumLength() !== null} maxlength="{$field->getMaximumLength()}"{/if}{if $field->getPlaceholder() !== null} placeholder="{$field->getPlaceholder()}"{/if}">
+
+{include file='__formFieldFooter'}
index 8e9be8dcd3cfe9b103d47cbb93397ced50d5b971..e28c4c03d2d09f556681e70986ddb3694a725e07 100644 (file)
@@ -12,6 +12,7 @@ use wcf\system\form\builder\field\dependency\ValueFormFieldDependency;
 use wcf\system\form\builder\field\IsDisabledFormField;
 use wcf\system\form\builder\field\TitleFormField;
 use wcf\system\form\builder\field\UserFormField;
+use wcf\system\form\builder\field\UsernameFormField;
 use wcf\system\form\builder\field\validation\FormFieldValidationError;
 use wcf\system\form\builder\field\validation\FormFieldValidator;
 use wcf\system\form\builder\field\BooleanFormField;
@@ -164,7 +165,9 @@ class DevtoolsFormBuilderTestForm extends AbstractForm {
                                                ->label('Tab 2')
                                                ->appendChildren([
                                                        SimpleAclFormField::create('objectAccess')
-                                                               ->label('Object can be accessed')
+                                                               ->label('Object can be accessed'),
+                                                       UsernameFormField::create('newUsername')
+                                                               ->label('A new username')
                                                ])
                                ])
                ]);
diff --git a/wcfsetup/install/files/lib/system/form/builder/field/UsernameFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/UsernameFormField.class.php
new file mode 100644 (file)
index 0000000..d1ff62a
--- /dev/null
@@ -0,0 +1,76 @@
+<?php
+namespace wcf\system\form\builder\field;
+use wcf\system\form\builder\field\validation\FormFieldValidationError;
+use wcf\util\UserUtil;
+
+/**
+ * Implementation of a form field to enter one non-existing username.
+ * 
+ * Usernames have a minimum length of 3 characters and a maximum length of 100 characters by default.
+ * 
+ * @author     Matthias Schmidt
+ * @copyright  2001-2018 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Form\Builder\Field
+ * @since      3.2
+ */
+class UsernameFormField extends AbstractFormField implements IMaximumLengthFormField, IMinimumLengthFormField, INullableFormField, IPlaceholderFormField {
+       use TMaximumLengthFormField;
+       use TMinimumLengthFormField;
+       use TNullableFormField;
+       use TPlaceholderFormField;
+       
+       /**
+        * @inheritDoc
+        */
+       protected $templateName = '__usernameFormField';
+       
+       /**
+        * Creates a new instance of `UsernameFormField`.
+        */
+       public function __construct() {
+               $this->maximumLength(100);
+               $this->minimumLength(3);
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function getSaveValue() {
+               if ($this->getValue() === null && !$this->isNullable()) {
+                       return '';
+               }
+               
+               return parent::getSaveValue();
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function readValue(): IFormField {
+               if (isset($_POST[$this->getPrefixedId()]) && is_string($_POST[$this->getPrefixedId()])) {
+                       $this->__value = $_POST[$this->getPrefixedId()];
+               }
+               
+               return $this;
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function validate() {
+               if ($this->getValue() === '' || $this->getValue() === null) {
+                       if ($this->isRequired()) {
+                               $this->addValidationError(new FormFieldValidationError('empty'));
+                       }
+               }
+               else if (!UserUtil::isValidUsername($this->getValue())) {
+                       $this->addValidationError(new FormFieldValidationError('invalid', 'wcf.form.field.username.error.invalid'));
+               }
+               else if (!UserUtil::isAvailableUsername($this->getValue())) {
+                       $this->addValidationError(new FormFieldValidationError('notUnique', 'wcf.form.field.username.error.notUnique'));
+               }
+               
+               parent::validate();
+       }
+}