简体   繁体   English

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

[英]TYPO3 v9: How to register a custom validator in backend

I am in the process of writing my own custom form validator, which checks if a submitted email already is in the database.我正在编写自己的自定义表单验证器,它检查提交的 email 是否已经在数据库中。

The Typo3 documentation explains pretty well, how you can do they here: https://docs.typo3.org/c/typo3/cms-form/main/en-us/I/Concepts/Validators/Index.html Typo3 文档解释得很好,你如何在这里做到这一点: https://docs.typo3.org/c/typo3/cms-form/main/en-us/I/Concepts/Validators/Index.html

Now the only problem I face is how to add my custom validator to the backend so the admin will be able to select it from the list of validators and maybe add a custom error message as an argument.现在我面临的唯一问题是如何将我的自定义验证器添加到后端,以便管理员能够从验证器列表中对其进行 select,并可能添加自定义错误消息作为参数。

I already registered a custom finisher.我已经注册了自定义修整器。 I believe registering a custom validator should be similar, just writing the right YAML configuration.我相信注册自定义验证器应该是类似的,只需编写正确的 YAML 配置即可。 However, I do not find any examples of how to do that.但是,我没有找到任何关于如何做到这一点的例子。

How to properly register a custom validator in a YAML config file so it shows up in the backend?如何在 YAML 配置文件中正确注册自定义验证器,以便它显示在后端?


Edit:编辑:

to avoid confusion:), I want to register the validator in the backend so the administrator is able to select it from the list of validators:为避免混淆:),我想在后端注册验证器,以便管理员能够从验证器列表中使用 select:

例子

Short answer: You have to "yaml yourself to hell" .简短的回答:你必须“让自己下地狱”

Long answer:长答案:

In addition to the validators definition, you have to add your validator to the propertyCollections and selectOptions of all form fields you want the validator to appear.除了验证器定义之外,您还必须将验证器添加到您希望验证器出现的所有表单字段的propertyCollectionsselectOptions中。 Selective, as eg a phone number validator does not make any sense in an email-field.选择性的,例如电话号码验证器在电子邮件字段中没有任何意义。

Have a look into the yaml configuration of the "Text" element:查看“文本”元素的 yaml 配置:

  1. Adding a new option to the options dropdown --> see the following lines向选项下拉列表添加新选项 --> 请参阅以下行
  2. Adding validators to the propertyCollection --> see the following lines将验证器添加到 propertyCollection --> 请参阅以下行

So you need to add a new option to the options dropdown and add your validator to the propertyCollection.因此,您需要向选项下拉列表添加一个新选项,并将您的验证器添加到 propertyCollection。

A working real life example, which adds four custom validators the the Date field:一个真实的工作示例,它在 Date 字段中添加了四个自定义验证器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM