R – Loading models in Zend_Form using Zend Framework

loadermodelzend-formzend-framework

I'm trying to build a form using the Zend_Form component, but the number of elements varies. The information for each Zend_Form element is stored in a database (name, options, validators, filters, etc.).

The application I'm working on consists of building surveys which contain a varying number of questions. Each question is associated with different arrays of answers. Ultimately my goal is to build arrays of radio/checkbox buttons, dynamically, server-side.

I'm looking for a pretty way to generate my form, but I'm not sure of the best way to load the model within the form. Should the model be loaded in the controller then passed (somehow, via a parameter?) directly to the form, or is it better to load the model within the Form init() method? Where's the best place to put the logic, should it be within the form class, or within the controller, or within the model?

My idea is to fetch form element properties (name, rules, filters, etc.) in the database, then iterate and finally render the form. What do you think of this approach? Ultimately, elements will be dynamically added (client-side), this time, using AJAX and a JavaScript library (such as jQuery).

Here are a couple useful links I found via Google, but I think they all answer a slightly different question than mine:

On building dynamic forms, server side:

On building dynamic forms, client side, with AJAX processing:

Best Answer

You could extend Zend_Form.

Zend form is not good place for logic, only form representation.

So, Load all needed elements using model in controller and pass them to the form in contructor as parameters.

Related Topic