I would prefer not to deal with decorators, as my form design is not entirely straightforward, but I would like to keep the functionality of form validation.
So, I set up where subforms work correctly, but when I try to create it manually in my viewcript, I get the name without a parent. I saw other posts similar, but I did not find a solution.
Example:
This is, in my opinion, a script
<?php echo $this->form->username->renderViewHelper();?>
Then i get
<input type="text" value="" id="username" name="username">
When rendering. It should be
<input type="text" value="" id="form1-username" name="form1[username]">
How do I get this part of form1?
Thanks!
Edit
Ok, so I found one way.
Using the useTo function, it works:
$form1->addElements(array( new Zend_Form_Element_Text('username', array( 'belongsTo' => 'form1', 'required' => true, 'label' => 'Username:', 'filters' => array('StringTrim', 'StringToLower'), 'validators' => array( 'Alnum', array('Regex', false, array('/^[az][a-z0-9]{2,}$/')) ) )) ));
Is there a better way to do this or is this the only way?
Edit2
public function prepareSubForm($spec){ if (is_string($spec)) { $subForm = $this->{$spec}; } elseif ($spec instanceof Zend_Form_SubForm) { $subForm = $spec; } else { throw new Exception('Invalid argument passed to ' . __FUNCTION__ . '()'); } $this->setSubFormDecorators($subForm) ->addSubmitButton($subForm) ->addSubFormActions($subForm); return $subForm; } public function setSubFormDecorators(Zend_Form_SubForm $subForm){ $subForm->setDecorators(array( 'FormElements', \\<--- I tried to change this to PrepareElements before. array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')), 'Form', )); return $this; }