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;
->label('Tab 2')
->appendChildren([
SimpleAclFormField::create('objectAccess')
- ->label('Object can be accessed')
+ ->label('Object can be accessed'),
+ UsernameFormField::create('newUsername')
+ ->label('A new username')
])
])
]);
--- /dev/null
+<?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();
+ }
+}