Remove submitted value from form if form validation fails

With the following (shortened) form I register new users in TYPO3 v12:

<f:form action="register" name="registration" object="{registration}" id="register-form">

    <f:form.textfield
        property="login"
        id="login" />

    <f:form.textfield
        type="password"
        property="password"
        id="password" />

</f:form>

The validation happens in the model:

class Registration extends TYPO3CMSExtbaseDomainObjectAbstractEntity
{
    /**
     * @var string
     * @ExtbaseValidate("NotEmpty")
     * @ExtbaseValidate("StringLength", options={"minimum": 8, "maximum": 30})
     */
    protected string $login = '';

    /**
     * @var string
     * @ExtbaseValidate("NotEmpty")
     * @ExtbaseValidate("RegularExpression", options={"regularExpression": "/^.*(?=.{8,})(?=.*[a-zA-Z])(?=.*d).*$/"})
     */
    protected string $password = '';
}

Submitting the form with less the eight characters for the login or password will show the form again. The form fields will show the submmited data and the error messages accordingly. This is the normal behaviour.

Now I don’t want to return the password to the form after it was submitted, the field shall be emptied.

I did the following with no luck:

  • I fidled with the controllers initializeAction, but when I delete the password here, the validator will always fail, since it gets the empty value.
  • I tried to set a default value for the form field, but this value will be overwritten with the submitted value.

Passing the password as name should do the job:

<f:form.textfield
    type="password"
    name="password"
    id="password" />

EDIT:
You can use:

<f:debug>{_all}</f:debug>

to see all variables. There should be an error, when the validation fails.
You can use this variable, to implement an if condition, to set the class of your textfield.

4

In order to use the normal model validation, you can use a custom viewhelper, which does not show the submitted value for the form field:

Viewhelper:

<?php

declare(strict_types=1);

namespace VendorExtensionViewHelpers;

use TYPO3CMSFluidViewHelpersFormAbstractFormFieldViewHelper;

/*
 * This is a modified version of `TYPO3CMSFluidViewHelpersFormTextfieldViewHelper`
 * It removes the value from the password field.
 *
 * Modifications are marked below.
 */
class PasswordViewHelper extends AbstractFormFieldViewHelper
{
    /**
     * @var string
     */
    protected $tagName = 'input';

    public function initializeArguments(): void
    {
        parent::initializeArguments();
        $this->registerTagAttribute('autofocus', 'string', 'Specifies that an input should automatically get focus when the page loads');
        $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
        $this->registerTagAttribute('maxlength', 'int', 'The maxlength attribute of the input field (will not be validated)');
        $this->registerTagAttribute('readonly', 'string', 'The readonly attribute of the input field');
        $this->registerTagAttribute('size', 'int', 'The size of the input field');
        $this->registerTagAttribute('placeholder', 'string', 'The placeholder of the textfield');
        $this->registerTagAttribute('pattern', 'string', 'HTML5 validation pattern');
        $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this ViewHelper', false, 'f3-form-error');
        $this->registerUniversalTagAttributes();
        $this->registerArgument('required', 'bool', 'If the field is required or not', false, false);
        $this->registerArgument('type', 'string', 'The field type, e.g. "text", "email", "url" etc.', false, 'text');
    }

    public function render(): string
    {
        $required = $this->arguments['required'];
        $type = $this->arguments['type'];

        $name = $this->getName();
        $this->registerFieldNameForFormTokenGeneration($name);
        $this->setRespectSubmittedDataValue(true);

        $this->tag->addAttribute('type', $type);
        $this->tag->addAttribute('name', $name);

        $value = $this->getValueAttribute();

        // Original of `TextfieldViewHelper`
        /*
        if ($value !== null) {
            $this->tag->addAttribute('value', $value);
        }
        */

        // Modification start
        $this->tag->addAttribute('value', '');
        // Modification end

        if ($required !== false) {
            $this->tag->addAttribute('required', 'required');
        }

        $this->addAdditionalIdentityPropertiesIfNeeded();
        $this->setErrorClassAttribute();

        // Modification start
        if ($value !== null) {
            if ($this->hasArgument('class')) {
                $cssClass = $this->arguments['class'] . ' ';
            } else {
                $cssClass = '';
            }
            $cssClass .= 'f3-form-error';
            $this->tag->addAttribute('class', $cssClass);
        }
        // Modification end

        return $this->tag->render();
    }
}

No you can use this viewhelper in the view:

<vendor:password
    type="password"
    property="password"
    id="password"
    class="form-control" />

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật