You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于TYPO3 8LTS SYSEXT:form自定义验证器实现示例请求

我来帮你搞定这个依赖验证需求!在TYPO3 8 LTS的系统表单扩展(SYSEXT:form)里,要完成「当复选框phone被勾选时,输入框phone为必填项」这个逻辑,需要自定义一个验证器,下面是完整的实现步骤:

步骤1:创建自定义验证器类

咱们先在自己的站点扩展(比如site_package)里创建验证器文件,路径为:Classes/Validation/Validator/RequiredIfCheckboxCheckedValidator.php

代码如下:

<?php
namespace Vendor\SitePackage\Validation\Validator;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;

class RequiredIfCheckboxCheckedValidator extends AbstractValidator
{
    /**
     * @var array
     */
    protected $supportedOptions = [
        'checkboxField' => [
            '',
            'The identifier of the checkbox field to check',
            'string',
            true
        ]
    ];

    /**
     * @param mixed $value The value that should be validated
     * @param FormRuntime $formRuntime
     */
    public function validate($value, $formRuntime = null)
    {
        parent::validate($value);

        if ($formRuntime === null) {
            return;
        }

        // 获取复选框字段的值
        $checkboxValue = $formRuntime->getFormState()->getFormValue($this->options['checkboxField']);
        
        // 如果复选框被勾选,且当前输入框值为空
        if ($checkboxValue && empty(trim($value))) {
            $this->addError(
                $this->translateErrorMessage(
                    'validator.requiredIfCheckboxChecked.error',
                    'site_package'
                ),
                1620000001
            );
        }
    }
}
步骤2:注册验证器到Form框架

在扩展的ext_localconf.php文件里添加注册代码,让TYPO3的Form系统能识别咱们的自定义验证器:

<?php
defined('TYPO3_MODE') || die();

call_user_func(function() {
    $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['validators']['Vendor.SitePackage:RequiredIfCheckboxChecked'] = [
        'class' => \Vendor\SitePackage\Validation\Validator\RequiredIfCheckboxCheckedValidator::class,
        'label' => 'LLL:EXT:site_package/Resources/Private/Language/locallang.xlf:validator.requiredIfCheckboxChecked.label'
    ];
});
步骤3:添加验证器的错误提示语言包

在Resources/Private/Language/locallang.xlf里添加对应的错误提示文本:

<?xml version="1.0" encoding="UTF-8"?>
<xliff version="1.0">
    <file source-language="en" datatype="plaintext" original="messages" date="2021-05-01T12:00:00Z">
        <body>
            <trans-unit id="validator.requiredIfCheckboxChecked.label">
                <source>Required if checkbox is checked</source>
            </trans-unit>
            <trans-unit id="validator.requiredIfCheckboxChecked.error">
                <source>This field is required when the corresponding checkbox is checked.</source>
            </trans-unit>
        </body>
    </file>
</xliff>
步骤4:在表单YAML配置中使用验证器

最后,在你的表单配置YAML文件里,给phone输入框添加这个自定义验证器,同时指定要检查的复选框字段标识符:

type: Form
identifier: contact-form
label: Contact Form
renderables:
  - type: Page
    identifier: page-1
    label: Page 1
    renderables:
      - type: Checkbox
        identifier: phone-checkbox  # 注意这个标识符要和验证器里的checkboxField对应
        label: I want to be contacted by phone
        defaultValue: false
      - type: Text
        identifier: phone
        label: Phone Number
        validators:
          - identifier: Vendor.SitePackage:RequiredIfCheckboxChecked
            options:
              checkboxField: 'phone-checkbox'  # 这里填复选框的标识符

注意事项

  • 替换代码里的Vendor\SitePackage为你自己的扩展厂商名和扩展名
  • 确保复选框和输入框的identifier在表单配置里唯一且对应
  • TYPO3 8 LTS的Form扩展依赖Extbase,所以验证器的写法要符合当时的规范

内容的提问来源于stack exchange,提问作者Mario Wilhelm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:20:10