TYPO3 v9:如何在后端注册自定义验证器

TYPO3 v9: How to register a custom validator in backend

我正在编写自己的自定义表单验证器,它会检查提交的电子邮件是否已在数据库中。

Typo3 文档解释得很好,您可以在此处进行操作:https://docs.typo3.org/c/typo3/cms-form/main/en-us/I/Concepts/Validators/Index.html

现在我面临的唯一问题是如何将我的自定义验证器添加到后端,以便管理员能够从验证器列表中 select 它并可能添加自定义错误消息作为参数。

我已经注册了自定义装订器。我相信注册自定义验证器应该是类似的,只需编写正确的 YAML 配置即可。但是,我找不到任何有关如何执行此操作的示例。

如何在 YAML 配置文件中正确注册自定义验证器,使其显示在后端?


编辑:

为了避免混淆 :),我想在后端注册验证器,以便管理员能够从验证器列表中select它:

简短回答:你必须“让你自己下地狱”

长答案:

除了验证器定义之外,您还必须将验证器添加到 propertyCollectionsselectOptions 您希望验证器出现的所有表单字段。选择性的,例如phone 数字验证器在 email-field.

中没有任何意义

查看“文本”元素的 yaml 配置:

  1. 向选项下拉列表添加新选项 --> 请参阅 following lines
  2. 向 propertyCollection 添加验证器 --> 请参阅 following lines

所以您需要在选项下拉列表中添加一个新选项,并将您的验证器添加到 propertyCollection。

一个真实的工作示例,它在日期字段中添加了四个自定义验证器:

TYPO3:
  CMS:
    Form:
      prototypes:
        standard:
          formElementsDefinition:
            Date:
              implementationClassName: 'Vendor\Extension\Domain\Model\FormElements\Date'
              properties:
                modifier: 'date'
              formEditor:
                editors:
                  900:
                    selectOptions:
                      30:
                        value: MinAge
                        label: 'Min Age'
                      35:
                        value: MaxAge
                        label: 'Max Age'
                      40:
                        value: MinDate
                        label: 'Min date from today'
                      50:
                        value: MaxDate
                        label: 'Max date from today'
                predefinedDefaults:
                  properties:
                    minage: ''
                    maxage: ''
                    minDateDays: null
                    maxDateDays: null
                propertyCollections:
                  validators:
                    2000:
                      identifier: MinAge
                      editors:
                        100:
                          identifier: header
                          templateName: Inspector-CollectionElementHeaderEditor
                          label: 'Min age (in years)'
                        250:
                          identifier: minAge
                          templateName: Inspector-TextEditor
                          label: 'Min age'
                          placeholder: 'Years (e.g. 17) as integer'
                          propertyPath: options.minage
                          propertyValidators:
                            10: Integer
                          additionalElementPropertyPaths:
                            10: properties.minage
                        350:
                          identifier: errorMessageCObjUid
                          templateName: Inspector-Typo3WinBrowserEditor
                          label: Error message relation
                          buttonLabel: formEditor.elements.ContentElement.editor.contentElement.buttonLabel
                          browsableType: tt_content
                          iconIdentifier: mimetypes-x-content-text
                          propertyPath: options.errorMessageCObjUid
                        9999:
                          identifier: removeButton
                          templateName: Inspector-RemoveElementEditor
                    2050:
                      identifier: MaxAge
                      editors:
                        100:
                          identifier: header
                          templateName: Inspector-CollectionElementHeaderEditor
                          label: 'Max age (in years)'
                        250:
                          identifier: maxAge
                          templateName: Inspector-TextEditor
                          label: 'Max age'
                          placeholder: 'Years (e.g. 25) as integer'
                          propertyPath: options.maxage
                          propertyValidators:
                            10: Integer
                          additionalElementPropertyPaths:
                            10: properties.maxage
                        350:
                          identifier: errorMessageCObjUid
                          templateName: Inspector-Typo3WinBrowserEditor
                          label: Error message relation
                          buttonLabel: formEditor.elements.ContentElement.editor.contentElement.buttonLabel
                          browsableType: tt_content
                          iconIdentifier: mimetypes-x-content-text
                          propertyPath: options.errorMessageCObjUid
                        9999:
                          identifier: removeButton
                          templateName: Inspector-RemoveElementEditor
                    2100:
                      identifier: MinDate
                      editors:
                        100:
                          identifier: header
                          templateName: Inspector-CollectionElementHeaderEditor
                          label: 'Min date from today'
                        250:
                          identifier: minDateDays
                          templateName: Inspector-TextEditor
                          label: 'Today + X days'
                          placeholder: 'Number of days'
                          propertyPath: options.minDateDays
                          propertyValidators:
                            10: Integer
                          additionalElementPropertyPaths:
                            10: properties.minDateDays
                        9999:
                          identifier: removeButton
                          templateName: Inspector-RemoveElementEditor
                    2200:
                      identifier: MaxDate
                      editors:
                        100:
                          identifier: header
                          templateName: Inspector-CollectionElementHeaderEditor
                          label: 'Max date from today'
                        250:
                          identifier: maxDateDays
                          templateName: Inspector-TextEditor
                          label: 'Today + X days'
                          placeholder: 'Number of days'
                          propertyPath: options.maxDateDays
                          propertyValidators:
                            10: Integer
                          additionalElementPropertyPaths:
                            10: properties.maxDateDays
                        9999:
                          identifier: removeButton
                          templateName: Inspector-RemoveElementEditor