Forms ZF3-在表单上的字段集合中设置字段

Forms ZF3-在表单上的字段集合中设置字段,forms,zend-framework2,zend-framework3,Forms,Zend Framework2,Zend Framework3,我有一个字段集,其中有一个名为“company”的字段,默认情况下不需要该字段。现在有一个表单添加了这个字段集,现在应该需要“company”。在e中有没有办法做到这一点。G表格工厂?或者我可以在绑定到表单的inputfilter类中重写字段集的输入过滤器吗 谢谢你的回复 字段集 class MyFieldset extends Fieldset implements InputFilterProviderInterface { public function init() {

我有一个字段集,其中有一个名为“company”的字段,默认情况下不需要该字段。现在有一个表单添加了这个字段集,现在应该需要“company”。在e中有没有办法做到这一点。G表格工厂?或者我可以在绑定到表单的inputfilter类中重写字段集的输入过滤器吗

谢谢你的回复

字段集

class MyFieldset extends Fieldset implements InputFilterProviderInterface {

    public function init() {
        $this->add([
            'name' => 'company',

            'options' => [
                'label' => 'Firma'
            ],

            'attributes' => [
                'id' => 'address-company'
            ]
        ]);
    }

    public function getInputFilterSpecification() {
        return [
            'company' => [
                'required' => false,

                'filters' => [
                    ['name' => 'StripTags'],
                    ['name' => 'StringTrim'],
                    ['name' => 'ToNull']
                ],

                'validators' => [
                    [
                        'name' => 'StringLength',

                        'options' => [
                            'encoding' => 'UTF-8',
                            'min' => 1,
                            'max' => 128
                        ]
                    ]
                ]
            ],
        ];
    }
}
表格

class MyForm extends Form {

    public function init() {
        $this->add([
            'type' => MyFieldset::class,
            'name' => 'test',

            'options' => [
                'use_as_base_fieldset' => true
            ]
        ]);
    }

}
表格工厂

class MyFormFactory implements FactoryInterface {

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
        $hydratorManager = $container->get('HydratorManager');
        $inputFilterManager = $container->get('InputFilterManager');

        $form = new MyForm();
        $form->setHydrator($hydratorManager->get(DoctrineObject::class));
        $form->setInputFilter($inputFilterManager->get(MyInputFilter::class));
        $form->bind(new Entity());

        return $form;
    }

}

我能想到的唯一办法就是

在您的字段集中使用:

private $companyRequired = FALSE;

public function setCompanyRequired($companyRequired)
{
    $this->companyRequired = (bool) $companyRequired;
}

public function getInputFilterSpecification()
{
    return [
        'company' => [
            'required' => $this->companyRequired,
            'filters' => [
                ['name' => 'StripTags'],
                ['name' => 'StringTrim'],
                ['name' => 'ToNull']
            ],
            'validators' => [
                [
                    'name' => 'StringLength',
                    'options' => [
                        'encoding' => 'UTF-8',
                        'min' => 1,
                        'max' => 128
                    ]
                ]
            ]
        ],
    ];
}
在需要公司的表单中,在添加字段集后,将其放入
init()
方法中:

$this->get('test')->setCompanyRequired(TRUE);

由于在验证表单之前不会读取
getInputFilterSpecification()
方法,因此这应该可以工作。

我唯一能想到的方法是

在您的字段集中使用:

private $companyRequired = FALSE;

public function setCompanyRequired($companyRequired)
{
    $this->companyRequired = (bool) $companyRequired;
}

public function getInputFilterSpecification()
{
    return [
        'company' => [
            'required' => $this->companyRequired,
            'filters' => [
                ['name' => 'StripTags'],
                ['name' => 'StringTrim'],
                ['name' => 'ToNull']
            ],
            'validators' => [
                [
                    'name' => 'StringLength',
                    'options' => [
                        'encoding' => 'UTF-8',
                        'min' => 1,
                        'max' => 128
                    ]
                ]
            ]
        ],
    ];
}
在需要公司的表单中,在添加字段集后,将其放入
init()
方法中:

$this->get('test')->setCompanyRequired(TRUE);

由于在验证表单之前不会读取
getInputFilterSpecification()
方法,因此这应该可以工作。

我之前已经回答过了

您可以在表单内部使用
Zend\InputFilter\InputFilterProviderInterface

class MyForm extends Form implements InputFilterProviderInterface
{

    public function init() 
    {
        //..
    }

    public function getInputFilterSpecification()
    {
        return [
            'test' => [
                'type' => 'Zend\\InputFilter\\InputFilter',
                'company' => [
                    'required' => true,
                ],
            ],
        ];
    }

}
或者,您也可以通过在
MyFormFactory
中手动配置字段集的输入过滤器来实现相同的功能

class MyFormFactory implements FactoryInterface 
{

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
    {
        //.. same code

        $form->getInputFilter()  // fetch the input filter you just attached.
            ->get('test')        // the name of the neseted fieldset (another input filter)
            ->get('company')     // the name of the input
            ->setRequired(true); // set required to true :)

        return $form;
    }

}

我以前已经回答过了

您可以在表单内部使用
Zend\InputFilter\InputFilterProviderInterface

class MyForm extends Form implements InputFilterProviderInterface
{

    public function init() 
    {
        //..
    }

    public function getInputFilterSpecification()
    {
        return [
            'test' => [
                'type' => 'Zend\\InputFilter\\InputFilter',
                'company' => [
                    'required' => true,
                ],
            ],
        ];
    }

}
或者,您也可以通过在
MyFormFactory
中手动配置字段集的输入过滤器来实现相同的功能

class MyFormFactory implements FactoryInterface 
{

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
    {
        //.. same code

        $form->getInputFilter()  // fetch the input filter you just attached.
            ->get('test')        // the name of the neseted fieldset (another input filter)
            ->get('company')     // the name of the input
            ->setRequired(true); // set required to true :)

        return $form;
    }

}
可以看看我的一个问题,并结合我的想法。第一个是关于将InputFilters与字段集分离。第二个是关于创建一个附加选项,允许您将字段集类型(因此另一个字段集)的输入设置为(不)必需。然后重写默认的isValid方法以允许该检查。这需要做一点工作,但会使使用字段集和集合更容易/更干净:)顺便说一句,关于ZF2的链接,但可能会有所帮助。可以看看我的一个问题,并将其与。第一个是关于将InputFilters与字段集分离。第二个是关于创建一个附加选项,允许您将字段集类型(因此另一个字段集)的输入设置为(不)必需。然后重写默认的isValid方法以允许该检查。这需要做一点工作,但会使使用字段集和集合更容易/更干净:)顺便说一句,关于ZF2的链接,但可能会有所帮助。