Dynamic forms using Zend Form

707 Views Asked by At

Hi guys I'm building a contacts application using the Zend Framework 1. I have a contacts table and a contact_data table.

Contact
|NAME|DESCRIPTION|...

CONTACT DATA
|TYPE|LOCATION|DETAILS|CONTACT_ID

ADDRESS
STREET|ZIP|CITY|STATE|COUNTRY|CONTACT_ID

The Contact Data holds all contact details such as Phone, Email, Fax etc and the address table is self explanatory. The trick is that I need to set it up so that I can add unlimited contact data and addresses. I've accomplished this earlier on by pretty much working on customised views however thats putting a lot of code logic within the view which I don't want. So I'm redoing it using Zend_Form but am stuck with regards to setting up the add/edit/remove multiple contact details from the same form part.

I have the javascript worked out and know how to get it done using views - but I need to get this done using Zend Forms here. I've looked at the idea of subforms however in my case I need to do the following:

My form is stuctured as follows:

Text and INputs for all contact details listed out

A special contact Data region with links to Add a Phone, Add a Fax, Add an address. Clicking on these links would open up and add a set of inputs to the table eg add an address link adds a street, city, country and state set of input to the table.

I've been hacking at this for an hour and am pretty lost here. Any ideas on how can i handle this?

1

There are 1 best solutions below

4
Lukasz Kujawa On

I think the best way to solve this problem is creating new Elements. One for contact data and one for address. Each of those elements should have custom viewHelper to render your complex interface. Each of view helpers can assign javascript file to handle dynamic adding / removing.

class Form_Element_Contact extends Zend_Form_Element_Xhtml {

    public $helper = 'contact';

}

class Zend_View_Helper_Contact extends Zend_View_Helper_FormElement {

    public function contact($name, $value = null, $attribs = null, $options = null) {
        $this->view->headScript()->appendFile("/js/dynamicInterfaceForContacts.js");
        $info = $this->_getInfo($name, $value, $attribs, $options);

        $html = $this->generateHtml(); // elements should be part of an array
        return $html;
    }

}

If you need more details let me know.