Add `PasswordFormField` (#4148)
authorMatthias Schmidt <gravatronics@live.com>
Mon, 26 Apr 2021 08:12:05 +0000 (10:12 +0200)
committerGitHub <noreply@github.com>
Mon, 26 Apr 2021 08:12:05 +0000 (10:12 +0200)
* Add `PasswordFormField`

* Fix typo in documentation of `PasswordFormField` constructor

Co-authored-by: Joshua Rüsweg <ruesweg@woltlab.com>
Co-authored-by: Joshua Rüsweg <ruesweg@woltlab.com>
com.woltlab.wcf/templates/__passwordFormField.tpl [new file with mode: 0644]
syncTemplates.json
wcfsetup/install/files/acp/templates/__passwordFormField.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/system/form/builder/field/PasswordFormField.class.php [new file with mode: 0644]

diff --git a/com.woltlab.wcf/templates/__passwordFormField.tpl b/com.woltlab.wcf/templates/__passwordFormField.tpl
new file mode 100644 (file)
index 0000000..202437b
--- /dev/null
@@ -0,0 +1,17 @@
+<input type="password" {*
+       *}id="{@$field->getPrefixedId()}" {*
+       *}name="{@$field->getPrefixedId()}" {*
+       *}value="{$field->getValue()}"{*
+       *}{if !$field->getFieldClasses()|empty} class="{implode from=$field->getFieldClasses() item='class' glue=' '}{$class}{/implode}"{/if}{*
+       *}{if $field->getInputMode() !== null} inputmode="{$field->getInputMode()}"{/if}{*
+       *}{if $field->getAutoComplete() !== null} autocomplete="{$field->getAutoComplete()}"{/if}{*
+       *}{if $field->getPattern() !== null} pattern="{$field->getPattern()}"{/if}{*
+       *}{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}{*
+       *}{if $field->getDocument()->isAjax()} data-dialog-submit-on-enter="true"{/if}{*
+       *}{foreach from=$field->getFieldAttributes() key='attributeName' item='attributeValue'} {$attributeName}="{$attributeValue}"{/foreach}{*
+*}>
index 620a176f0cf84e0f4e82d58fe66a83ab7a207927..60709bd8678a05920c40a425bc3ad66f9a08c48f 100644 (file)
@@ -33,6 +33,7 @@
     "__multipleSelectionFormField",
     "__nonEmptyFormFieldDependency",
     "__numericFormField",
+    "__passwordFormField",
     "__pollOptionsFormField",
     "__radioButtonFormField",
     "__ratingFormField",
diff --git a/wcfsetup/install/files/acp/templates/__passwordFormField.tpl b/wcfsetup/install/files/acp/templates/__passwordFormField.tpl
new file mode 100644 (file)
index 0000000..202437b
--- /dev/null
@@ -0,0 +1,17 @@
+<input type="password" {*
+       *}id="{@$field->getPrefixedId()}" {*
+       *}name="{@$field->getPrefixedId()}" {*
+       *}value="{$field->getValue()}"{*
+       *}{if !$field->getFieldClasses()|empty} class="{implode from=$field->getFieldClasses() item='class' glue=' '}{$class}{/implode}"{/if}{*
+       *}{if $field->getInputMode() !== null} inputmode="{$field->getInputMode()}"{/if}{*
+       *}{if $field->getAutoComplete() !== null} autocomplete="{$field->getAutoComplete()}"{/if}{*
+       *}{if $field->getPattern() !== null} pattern="{$field->getPattern()}"{/if}{*
+       *}{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}{*
+       *}{if $field->getDocument()->isAjax()} data-dialog-submit-on-enter="true"{/if}{*
+       *}{foreach from=$field->getFieldAttributes() key='attributeName' item='attributeValue'} {$attributeName}="{$attributeValue}"{/foreach}{*
+*}>
diff --git a/wcfsetup/install/files/lib/system/form/builder/field/PasswordFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/PasswordFormField.class.php
new file mode 100644 (file)
index 0000000..7785633
--- /dev/null
@@ -0,0 +1,106 @@
+<?php
+
+namespace wcf\system\form\builder\field;
+
+use wcf\system\form\builder\field\validation\FormFieldValidationError;
+
+/**
+ * Implementation of a form field for a password.
+ *
+ * @author  Matthias Schmidt
+ * @copyright   2001-2021 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package WoltLabSuite\Core\System\Form\Builder\Field
+ * @since   5.4
+ */
+class PasswordFormField extends AbstractFormField implements
+    IAttributeFormField,
+    IAutoCompleteFormField,
+    IAutoFocusFormField,
+    ICssClassFormField,
+    IImmutableFormField,
+    IInputModeFormField,
+    IMaximumLengthFormField,
+    IMinimumLengthFormField,
+    IPatternFormField,
+    IPlaceholderFormField
+{
+    use TInputAttributeFormField;
+    use TTextAutoCompleteFormField;
+    use TAutoFocusFormField;
+    use TCssClassFormField;
+    use TDefaultIdFormField;
+    use TImmutableFormField;
+    use TInputModeFormField;
+    use TMaximumLengthFormField;
+    use TMinimumLengthFormField;
+    use TPatternFormField;
+    use TPlaceholderFormField;
+
+    /**
+     * @inheritDoc
+     */
+    protected $javaScriptDataHandlerModule = 'WoltLabSuite/Core/Form/Builder/Field/Value';
+
+    /**
+     * @inheritDoc
+     */
+    protected $templateName = '__passwordFormField';
+
+    /**
+     * Creates a new instance of `PasswordFormField`.
+     */
+    public function __construct()
+    {
+        $this->label('wcf.user.password');
+        $this->addFieldClass('long');
+    }
+
+    /**
+     * @inheritDoc
+     */
+    protected function getValidInputModes(): array
+    {
+        return [
+            'text',
+        ];
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public function readValue()
+    {
+        if ($this->getDocument()->hasRequestData($this->getPrefixedId())) {
+            $this->value = $this->getDocument()->getRequestData($this->getPrefixedId());
+        }
+
+        return $this;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public function validate()
+    {
+        $value = $this->getValue();
+        $hasValue = $this->getValue() !== null && $this->getValue() !== '';
+
+        if ($this->isRequired() && !$hasValue) {
+            $this->addValidationError(new FormFieldValidationError('empty'));
+        } elseif ($hasValue) {
+            $this->validateMinimumLength($value);
+            $this->validateMaximumLength($value);
+        }
+
+        parent::validate();
+    }
+
+    /**
+     * @inheritDoc
+     */
+    protected static function getDefaultId()
+    {
+        return 'password';
+    }
+}