Zend_Form array designation without indexes

I would like to create a form that allows the user to enter any number of values, each in a separate text field, using array notation. An example of the expected HTML output:

<dd id="dupa-element"> <input type="text" name="dupa[]" value=""> <input type="text" name="dupa[]" value=""> </dd> 

However, I cannot find a way to introduce multiple input elements into one element using array notation without indexes.

I am currently doing this:

 $elt1 = new Zend_Form_Element_Text('1'); $elt1->setOptions(array('belongsTo' => 'dupa')); $elt2 = new Zend_Form_Element_Textarea('2'); $elt2->setOptions(array('belongsTo' => 'dupa')); 

While this works, Zend_Form treats them as independent elements (which can have different sets of validators and filters - this is cool), and the resulting HTML is something like that:

 <dd id="dupa-1-element"> <input type="text" name="dupa[1]" id="dupa-1" value=""> </dd> <dd id="dupa-2-element"> <input type="text" name="dupa[2]" id="dupa-2" value=""> </dd> 

Is there a (preferably simple) way to get the item nomenclature I am after?

+6
source share
3 answers

I would complete the MWOP tutorial on building elements . More work, but less Trial & Error, then akond solution. The main idea for me would be to extend Zend_Form_Element_Multi (what you want is how Zend_Form_Element_Multiselect / MultiCheckbox works).

+3
source

I managed to do this by having a โ€œsubformโ€ of the container and then adding subforms to this โ€œcontainerโ€, for example:

 $container = new Zend_Form_SubForm(); $subform1 = new Zend_Form_SubForm(); $container->addSubForm($subform1, '1'); $subform2 = new Zend_Form_SubForm(); $subform2->addSubForm($subform1, '2'); $mainForm = new Zend_Form(); $mainForm->addSubform($container,'mysubforms'); 

Hope this helps.

+2
source

For this you need a special assistant.

 class Zend_View_Helper_FormMySelect extends Zend_View_Helper_Abstract { function formMySelect ($name, $value = null, $attribs = null, $options = null, $listsep = "<br />\n") { $result = array (); foreach ($options as $option) { $result [] = sprintf ('<input type="text" name="%s[]" value="">', $name); } return join ($listsep, $result); } } 

Than in your form something like this:

  $form = new Zend_Form(); $form->addElement ('select', 'test', array ( 'label' => 'Test', 'multioptions' => array ( 'test 1', 'test 2', 'test 3', ), )); $form->test->helper = 'formMySelect'; 
+1
source

Source: https://habr.com/ru/post/888397/


All Articles