ZF3 - 在字段集中设置字段需要在表单上展开

时间:2017-09-01 07:44:33

标签: forms zend-framework2 zend-framework3

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

感谢您的回复!

字段集

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;
    }

}

2 个答案:

答案 0 :(得分:1)

我能想到的唯一方法是。

在您的字段集中使用:

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()方法,因此这应该有效。

答案 1 :(得分:1)

我之前已回复a similar question relating to over loading the default input filter options of a nested fieldset

您可以使用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;
    }

}