I've got a form class that dynamically creates fieldsets with elements/fieldsets recursively. I do this to get settings[general][rpp][value]
as an input name (for example). The fields get generated because the settings are user defined in an XML file.
Fieldsets are getting created like this:
$fieldset = new Fieldset(...);
$fieldset->add(...);
$form->add($fieldset);
The form is being output correctly; everything works. Except i need validation.
My goal is to define validators and filters for these nested elements. I'm really confused at how it works - but it looks like just the form itself defines an input_filter setInputFilter(...)
and i don't know how to get it to recognize the recursion without a factory and proprietary classes for the fieldsets instead of being dynamic.
Am i clear?
Thanks.
I've figured out how to do this highly dynamic type of form with validation and filters. I will explain here with this hypothetical script:
// create a form instance and a filter instance
$form = new Form();
$filter = new InputFilter();
// create a fieldset instance and another filter instance
$fieldset_a = new Fieldset('general');
$fieldset_a_filter = new InputFilter();
// create element(s) to assign to fieldset
$setting_1 = new Element('setting_1');
// create another input filter for element defining filters and validators
$setting_1_filter = new InputFilter(array(
'name' => 'setting_1',
'required' => true,
'validators' => array(), // ...
));
// add element to fieldset
$fieldset_a->add($setting_1);
// add fieldset to form
$form->add($fieldset_a);
// add element filter to fieldset filter
$fieldset_a_filter->add($setting_1_filter,'setting_1');
// add fieldset A filter to main input filter
$filter->add($fieldset_a_filter,'general');
$form->setInputFilter($filter);
So you can see you have to create input filters for each set of elements and each fieldset and then work backwards through them adding them to each other until the main input filter is built and you can assign it to the form instance.
This then would use the supplied validators with input names such as general[setting_1]
after running $form->setData($this->request->getPost())
- $form->isValid()
This response could be 100 times more detailed, but it's better than what is available on the subject of dynamic fieldset validation.
here, in the ZF2 docs is explained as well http://framework.zend.com/manual/2.3/en/modules/zend.form.collections.html