Php 如何从另一个表单字段设置表单字段的值?

Php 如何从另一个表单字段设置表单字段的值?,php,zend-framework,zend-framework2,zend-form,zend-inputfilter,Php,Zend Framework,Zend Framework2,Zend Form,Zend Inputfilter,例如,我们有一个包含两个字段、筛选器和验证器的表单: $factory = new \Zend\Form\Factory(); $form = $factory->createForm([ 'elements' => [ [ 'spec' => [ 'name' => 'fieldOne', 'type' => 'Text', ]

例如,我们有一个包含两个字段、筛选器和验证器的表单:

$factory = new \Zend\Form\Factory();
$form = $factory->createForm([
    'elements' => [
        [
            'spec' => [
                'name' => 'fieldOne',
                'type'  => 'Text',
            ],
        ],
        [
            'spec' => [
                'name' => 'fieldTwo',
                'type' => 'Text',
            ],
        ],
    ],
    'input_filter' => [
        'fieldOne' => [
            'filters' => [
                ['name' => 'StringTrim'],
            ],
            'validators' => [                      
                new \Application\Validator\FieldOneValidator(),
            ],
        ],
        'fieldTwo' => [
            'filters' => [
                ['name' => 'StringToUpper'],
            ],
        ],
    ],
]);
如果
fieldOne
有效且
fieldTwo
为空,则需要将过滤值表单
fieldOne
设置为
fieldTwo
,并对其进行过滤

$form->setData([
    'fieldOne' => '    test    ',
    'fieldTwo' => '',
]);
if ($form->isValid()) {
    $form->getData(); // ['fieldOne' => 'test', 'fieldTwo' => 'TEST']
}

$form->setData([
    'fieldOne' => '    test    ',
    'fieldTwo' => 'not empty',
]);
if ($form->isValid()) {
    $form->getData(); // ['fieldOne' => 'test', 'fieldTwo' => 'NOT EMPTY']
}

如何实现这一点?

看看“相同”验证器:

在验证器中,有第二个参数
$context
传递给isValid,它为您提供表单中的值

在这里,您需要利用其他字段的填充方式来验证当前字段


换句话说,创建一个自定义验证器,并在验证器中使用“上下文”,将其附加到具有依赖项的字段。

查看“相同”验证器:

在验证器中,有第二个参数
$context
传递给isValid,它为您提供表单中的值

在这里,您需要利用其他字段的填充方式来验证当前字段


换句话说,创建一个自定义验证器,并在验证器中使用“上下文”,将其附加到具有依赖项的字段。

验证器只是验证值,但我需要使用上下文对其进行更改。验证器只是验证值,但我需要使用上下文对其进行更改。