In zend framework, how do I add an inputfilter for a new form element in a fieldset with the code in the form? In the following case, I've defined a set of common form elements and an inputfilter for those elements in a fieldset class, and add the fieldset to the form. After that, I add one or more new form elements to the fieldset in the form code (I'm doing it in the form and not in the fieldset to prepare to add elements dynamically by form factory). What I'm having trouble with is adding new inputfilter definitions for the additional elements.
in my fieldset:
class myFieldset extends Fieldset implements InputFilterProviderInterface
{
public function init()
{
// add form elements
}
public function getInputFilterSpecification()
{
$inputFilter['myFormElement'] = [
'required' => true,
'filters' => [[ ... ],],
'validators' => [[ ... ],],
'allow_empty' => false,
'continue_if_empty' => false,
];
// more filters
return $inputFilter;
}
}
in my form:
class myForm extends Form
{
public function __construct()
{
// ...
// add elements from fieldset
$this->add([
'name' => 'myFieldset',
'type' => 'Application\Form\Fieldset\myFieldset',
'options' => [
'use_as_base_fieldset' => true,
],
]);
// add new element
$myFieldset = $this->get('myFieldset');
$myFieldset->add([
'name' => 'additionalElement',
'type' => 'Zend\Form\Element\ ... ',
'attributes' => [],
'options' => [],
]);
// update inputfilter
$input = new Input('additionalElement');
$input->setRequired(false);
$currentInputFilter = $this->getInputFilter();
$currentInputFilter->add($input);
$this->setInputFilter($currentInputFilter);
// submit buttons
}
}
In this example, the additional element gets added to the fieldset, but I've got the code wrong to add new definitions to the inputfilter.
You need to get the Input Filter of the Fieldset and not of the form class. For this Zend Framework holds the
InputFilterProviderFieldset
class, from which you should inherit your fieldset. TheInputFilterProviderFieldset
class comes with getter and setter methods for modifying your input filter specifications at the runtime. The following code is not tested, but should work.With using the
InputFilterProviderFieldset
class, your form class should look like this.As you can see Zend Framework brings all the stuff you need for solving your issue. I hope this approach will help you.