How to create a subform of a form - Zend Framework
I have a form with an entire field that displays an object. From this I would generate a subformat inside the Form class. I am trying to do this with the display group, but when I call "subform" in the controller, the form tag is not generated. how can i solve? Thanks to
this is the code.
<?php
$username = new Zend_Form_Element_Text('user');
...//param for field
$password = new Zend_Form_Element_Password('pwd');
...//param for field
$name = new Zend_Form_Element_Text('name');
...//param for field
$submit = new Zend_Form_Element_Submit('submit');
...//param for field
$this->addElement(array($user,$password,$name,$submit));
$this->addDisplayGroup(array($user,$password,$submit),'login');
$this->addDisplayGroup(array($user,$password,$name, $submit),'create');
?>
source to share
The subformation is different from the display group. A subform is an instance Zend_Form_SubForm
nested within an instance Zend_Form
. You can use this to insert one shape into another. For example, you might have a user profile form and a registration form. In the registration form, you can enter profile values, as well as some other data. Thus, you can use this profile form as a subform embedded in the registration form. Subform is mainly used for DRY (don't repeat yourself) principles or for creating multi-page forms.
A display group is simply a visual representation of some form elements grouped together. In html syntax this is called fieldset
. The main goal is to create groups of elements that belong to each other. For example, in a shopping cart, you might have an address group of accounts and a group of shipping addresses. Such a display group is mainly used for semantics and visual presentation.
In regards to the biggest differences, for display groups, the form has an idea of these form elements, since with subforms the form has no idea of the subform elements. That being said, I noticed that you want to create one form containing two display groups: one at login, when you create (or register) a user. With the above, you cannot use mapped groups for this. One option is to use two instances of the form:
class LoginForm extends Zend_Form
{
public function init ()
{
$this->addElement('text', 'user');
$this->addElement('password', 'pwd');
$this->addElement('submit', 'submit');
}
}
class RegisterForm extends Zend_Form
{
public function init ()
{
$this->addElement('text', 'user');
$this->addElement('password', 'pwd');
$this->addElement('text', 'name');
$this->addElement('submit', 'submit');
}
}
If you want to re-use fields user
, and pwd
you can use this subform:
class BaseForm extends Zend_Form_SubForm
{
public function init ()
{
$this->addElement('text', 'user');
$this->addElement('password', 'pwd');
}
}
class LoginForm extends Zend_Form
{
public function init ()
{
$subform = new BaseForm;
$this->addSubform($subform, 'base');
$this->addElement('submit', 'submit');
}
}
class RegisterForm extends Zend_Form
{
public function init ()
{
$subform = new BaseForm;
$this->addSubform($subform, 'base');
$this->addElement('text', 'name');
$this->addElement('submit', 'submit');
}
}
In both cases, you can simply create one of these forms in your controller:
public function loginAction ()
{
$form = new LoginForm();
// More code here
$this->view->form = $form;
}
public function registerAction ()
{
$form = new RegisterForm();
// More code here
$this->view->form = $form;
}
source to share
Zend_Form_SubForm
does not display tags by default <form>
.
To do this, you need to add a decorator 'Form'
to your subform instance before you execute it.
Try:
$mySubForm->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
->addDecorator('Form');
and then the script you think you can do:
<?php echo $this->mySubForm; ?>
source to share