Zend script render subform view

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; } 
+4
source share
1 answer

I believe that you can get the desired result simply by using

 <?php echo $this->form->username; ?> 

I get the expected result when calling this without renderViewHelper. It is also without special code for decorators or subform preparation. All I had to do was add belongsTo to the form element.

UPDATED:

If you set it as the default decorator, you can exclude dd / dt tags from rendering, it will use a div instead. Then you can be closer to getting the user output you want. You can change the tag in the HtmlTag from the div to any tag that you want to include in your elements. This is what I mainly use:

 array( 'ViewHelper', 'Errors', array('Description', array('tag' => 'p', 'class' => 'description')), array('HtmlTag', array('tag' => 'div', 'class' => 'form-div')), array('Label', array('class' => 'form-label', 'requiredSuffix' => '*')) ); 

This is the default value for Zend Framework:

 array( 'ViewHelper', 'Errors', array('Description', array('tag' => 'p', 'class' => 'description')), array('HtmlTag', array('tag' => 'dd', 'id' => array('callback' => $getId))) array('Label', array('tag' => 'dt')) ); 

Note that different decorators are used in the files and submit / button elements.

Also see this answer

+1
source

All Articles