ZF3 - Set field in fieldset required depnding on form

1k Views Asked by At

I have a fieldset with a field called "company" which is not required by default. Now there is a form which adds this fieldset and now "company" should be required. Is there a way to do this in e. g. the form factory? Or can I rewrite the input filters of the fieldset in my inputfilter class binded to the form?

Thanks for any response!

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

Form

class MyForm extends Form {

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

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

}

Form Factory

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

There are 2 best solutions below

0
On

The only way I can think of doing it is.

In your fieldset use:

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

in the form where the company is required place this in your init() method after adding the fieldset:

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

As the getInputFilterSpecification() method is not read until you validate your form this should work.

0
On

I have previously answered a similar question relating to over loading the default input filter options of a nested fieldset.

You can do the same inside the form using the Zend\InputFilter\InputFilterProviderInterface

class MyForm extends Form implements InputFilterProviderInterface
{

    public function init() 
    {
        //..
    }

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

}

Or alternately, you can achieve the same by manually configuring the fieldset's input filter inside 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;
    }

}