-3

有没有人有在 Shopware 6 中使用额外的必填字段扩展注册表单的经验?我很难让它正常工作,因为即使我添加的所有必填字段都是空的,也可以提交注册表单。

实施我自己的表单违规的正确方法是什么?我已经确保将 required="required" 添加到输入中。我已经看到它在树枝模板中以两种不同的方式完成了注册,但它们都不适合我。

4

1 回答 1

0

我自己尝试过,添加一个表单组件required="required"是否足以进行前端验证:

  • 在顶部添加(用于测试)vendor/shopware/storefront/Resources/views/storefront/component/address/address-personal-company.html.twig

      <div class="form-now">
          {% block component_address_form_company_legal_label %}
              <label class="form-label"
                     for="{{ prefix }}legal">
                  {{ "address.companyLegalLabel"|trans|sw_sanitize }}{% if accountTypeRequired %}{{ "general.required"|trans|sw_sanitize }}{% endif %}
              </label>
          {% endblock %}
    
          {% block component_address_form_company_legal_input %}
              <input type="text"
                     class="form-control{% if violationPath %} is-invalid{% endif %}"
                     id="{{ prefix }}company"
                     placeholder="{{ "address.companyLegalPlaceholder"|trans|striptags }}"
                     name="{{ prefix }}[company]"
                     value="{{ address.get('legal') }}"
                     required="required">
          {% endblock %}
    
          {% block component_address_form_company_legal_input_error %}
              {% if violationPath %}
                  {% sw_include '@Storefront/storefront/utilities/form-violation.html.twig' %}
              {% endif %}
          {% endblock %}
      </div>
    

该字段已验证,我无法提交表单(如果我填写了除自定义字段之外的所有内容)

验证错误

  • 确保你有 _input_error_component

  • 此外,您还应该始终在后端验证您的数据,这样任何可以使用浏览器开发工具删除必填字段的人都无法覆盖您的验证。

    • 可以通过装饰 \Shopware\Core\Checkout\Customer\SalesChannel\RegisterRoute::register并将附加字段传递给参数$additionalValidationDefinitions并调用原始函数来添加此类验证。
于 2022-02-06T07:48:53.640 回答