Using the Attachment Decorator in Nested Subforms (Zend Form)

I want to use a script view to render my zend form, as this seems to be the best way to control the layout / design of the form while still using the Zend_Elements classes.

From the script view, I create an element with $this->element->getElement('elementName') .

I'm having issues with item names. This is actually a sub-form inside a subform inside a form.

When I used FormElements decorators, the full name of the elements was formed [subForm] [subForm] [element], which was good. Wehn I switched to viewScript decorators, it changed to subForm [subForm] [element].

I realized that I needed to use the PrepareElements decorator to fix this, but it made the name change the form [subForm] [form] [subForm] [subForm] [elements] (it doubled the first two names at the beginning).

Any ideas how I should handle this?

Thanks.

UPDATE: I tried to debug PrepareElements, and I really don't understand what it is doing. It seems to work fine in the first iteration, but then it again adds the prefix of the form [subform] when working on one of the middle subforms.

When I do not use the PrepareElements decorator, I simply skip the "form" prefix in the names (that is, instead of the form [subForm] [element], I get only subForm [element]).

Maybe I can somehow fix this?

I tried to change the belongsTo property, but only replaced the prefix "subForm".

Actually, it seems that the applyTo method in subForm is missing.

Again, all this is due to the ViewScript decorator. It works great with FormElements decorators.

UPDATE 2: Just to clarify, I would not mind this name change, but it causes my fields to not populate when I call form-> populate.

Edit: I think I narrowed down the problem: when I return my values โ€‹โ€‹to setDefaults, they are ordered as follows:

 array( \"formElements1-name\" => value1... \"subFormName\" => array( \"parentFormName\" => array( \"subFormName\" => subForm-values-array ) ) 

... The main problem here is that "parentFormName" => "subFormNAme".. what does it repeat? I'm already in top form. I suppose this is because I set setElementsBelongTo(formName[subFormName]) , but if I didnโ€™t, then I would get my subform values โ€‹โ€‹completely separate from the form,

i.e. values โ€‹โ€‹array = array (\ "formName \" => array (formValues), \ "subFormNAme \" => array (subFormValues)

and I exepct him

 array( formName => array( subFormNAme => values-array ) )... 

Is it possible to make this work?

+6
source share
4 answers

The current solution is to use the PrepareElements decorator in subforms with one change - to remove the recursive call in PrepareElements code. In addition, "setElementsBelongTo" is not required.

It seems that they are generating the correct names and identifiers.

+2
source

You are just trying to display your form with <?php echo $this->form; ?> <?php echo $this->form; ?> from your view of the script?

This works well for simple forms, but for my more complex forms, I try to display each element individually, but I do not need to use a ViewScript decorator for each individual element. Just try something like this from your view of the script:

 <div class="form"> <fieldset> <legend>Some Form Name</legend> <form action="<?php echo $this->escape($this->form->getAction()) ?>" method="<?php echo $this->escape($this->form->getMethod()) ?>" enctype="multipart/form-data"> <?php echo $this->form->id; // render the id element here ?> <div class="half"> <?php echo $this->form->name; // render the user name field here ?> </div> <div class="half"> <?php echo $this->form->description; // render the description element here ?> </div> <div class="clear"></div> <div class="half"> <?php echo $this->form->address1; // render the address ?> </div> <div class="half"> <?php echo $this->form->address2; // render address2 ?> </div> <div class="clear"></div> <div class="third"> <?php echo $this->form->zip; // render zip code ?> </div> <div class="third"> <?php echo $this->form->city; // render city ?> </div> <div class="third"> <?php echo $this->form->state; // render state ?> </div> <div class="clear"></div> <div class="half"> <?php echo $this->form->country; // render country ?> </div> <div class="clear"></div> <?php echo $this->form->submit; ?> </form> </fieldset> </div> 

This is how I do most of my forms, because I want some elements to occupy half the width and others to take the entire width.

Surprisingly, the reference guide does not tell you that you can do this. It seems that I remember the page about this in the past, but I can not find it now. When I started working with the Zend Framework, I thought that the only way to get my form for output exactly the way I wanted is to create complex decorators, but that is not the case.

Matthew Weyer O'Phinney has an excellent blog post rendering the Zend_Form decorators individually , which explains what I did above. I hope they add this to the front page of the Zend Form, because it discouraged me at first. The fact is that 90% of my forms visualize elements individually, and not just the echo form.

Note. To prevent ZF from including form elements in the dt and dd tags, I apply this decorator to all of my standard form elements. I have a base class class from which I distribute all my forms, so I don't need to repeat this everywhere. This is a decorator for an element, so I can use tags to include my elements.

 public $elementDecorators = 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' => '*')) ); 

For my submit buttons, I use

 public $buttonDecorators = array( 'ViewHelper', array('HtmlTag', array('tag' => 'div', 'class' => 'form-button')) ); 
+8
source

The solution is to use the property of the belongsTo() form element.
Example:

 new Zend_Form_Element_Text('<elementName>', array('belongsTo' => '<subformName>')) 

Thus, the render () method will use the name of the form element, for example

 name="<subformName>[<elementName>]" 
0
source

I had the same problem and solved it with a decorator

1: create a common subformation with elements

2: Using a special decorator with PrepareElements

3: Change the form to an array using setIsArray (true)

Example:

The form

 $i = 4; for($i = 0; $i < $nbReclam ; $i++) { $rowForm = new Zend_Form_SubForm($i); $name= new Zend_Form_Element_Textarea('name'); $rowForm->addElement($name); $this->addSubForm($rowForm, $i); } $this->setDecorators(array( 'PrepareElements', array('ViewScript', array('viewScript' => 'myDecorator.phtml')), )); $this->setIsArray(true); 

decorator

 <table> <thead> <tr> <th>Nยฐ</th> <th>Name</th> </tr> </thead> <tbody> <?php foreach ($this->element->getSubForms() as $subForm) : ?> <tr> <td> <?php echo $i++?> </td> <?php foreach ($subForm->getElements() as $row) : ?> <td><?php echo $row ?></td> <?php endforeach ?> </tr> <?php endforeach ?> </tbody> </table> 

Enjoy

Sorry for my english i'm french

0
source

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


All Articles