How to generate subform of form - Zend Framework

Marco Barrella picture Marco Barrella · Mar 18, 2012 · Viewed 9.2k times · Source

I have a form with all field that map an object. Of this, i would generate a subform within of the Form class. I'm trying to do this by the displaygroup, but when call the "subform" in the controller, the tag form, is not generate. how can I solve ? Thanks

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');
?>

Answer

Jurian Sluiman picture Jurian Sluiman · Mar 19, 2012

A subform is something different than a display group. A subform is a Zend_Form_SubForm instance nested in Zend_Form instance. You can use this to embed one form into another. As an 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 details. So, you can use this profile form as subform embedded inside the registration form. A subform is mainly used for DRY (don't repeat yourself) principles or to create a multi-page form.

A display group is just a visual representation of some form elements grouped together. In html syntax this is called a fieldset. The main purpose is to create groups of elements which belong to each other. For example in a shopping cart you might have an invoice address group and a shipping address group. Such display group is mainly used for semantics and visual representation.

On of the largest differences is that for display groups, the form has awareness of those form elements, as with subforms the form has no awareness of the elements of the subforms. This said, I notice you want to create one form which contains two display groups: one when you login, one when you create (or register) a user. With the given from above you cannot use display groups for this. One option is to use two form instances:

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 reuse the fields user and pwd you might want to use subforms for this:

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 instantiated one of those 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;
}