From de2accce654a210a89a8fe43e8a2c33b828b8cca Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Mon, 26 Apr 2021 10:12:05 +0200 Subject: [PATCH] Add `PasswordFormField` (#4148) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * Add `PasswordFormField` * Fix typo in documentation of `PasswordFormField` constructor Co-authored-by: Joshua Rüsweg Co-authored-by: Joshua Rüsweg --- .../templates/__passwordFormField.tpl | 17 +++ syncTemplates.json | 1 + .../acp/templates/__passwordFormField.tpl | 17 +++ .../builder/field/PasswordFormField.class.php | 106 ++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 com.woltlab.wcf/templates/__passwordFormField.tpl create mode 100644 wcfsetup/install/files/acp/templates/__passwordFormField.tpl create mode 100644 wcfsetup/install/files/lib/system/form/builder/field/PasswordFormField.class.php diff --git a/com.woltlab.wcf/templates/__passwordFormField.tpl b/com.woltlab.wcf/templates/__passwordFormField.tpl new file mode 100644 index 0000000000..202437bcca --- /dev/null +++ b/com.woltlab.wcf/templates/__passwordFormField.tpl @@ -0,0 +1,17 @@ +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/syncTemplates.json b/syncTemplates.json index 620a176f0c..60709bd867 100644 --- a/syncTemplates.json +++ b/syncTemplates.json @@ -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 index 0000000000..202437bcca --- /dev/null +++ b/wcfsetup/install/files/acp/templates/__passwordFormField.tpl @@ -0,0 +1,17 @@ +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 index 0000000000..7785633be9 --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/PasswordFormField.class.php @@ -0,0 +1,106 @@ + + * @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'; + } +} -- 2.20.1