1

Topic: Dynamic Forms?

Hi there.

I'm working on a custom registration/poll system which basicly stores forms and filled out forms i a database and shows this in structured order.

I wonder if it is possible to use Zend Forms to make the forms from the information in the database (fieldname, type, required etc.) or do I need to make something custom?

I can explain more if needed. smile

2

Re: Dynamic Forms?

If you mean create a form automagically from database info we just had a discussion about this here and as far as I know nothing like that is implemented.
If you are wondering how to populate a zend form with stuff from a database look into the Zend_Form->populate() function.

3

Re: Dynamic Forms?

I mean the first you suggested. What did the discussion end with? Do you think it is possible to do?

The last suggestion is something I'm going to need, but haven't really thought about it before so I'll definitely be looking into that as well.

4

Re: Dynamic Forms?

harri wrote:

I mean the first you suggested. What did the discussion end with? Do you think it is possible to do?

The last suggestion is something I'm going to need, but haven't really thought about it before so I'll definitely be looking into that as well.

I wasn't personally involved in it but you could find it her in the forums its fairly recent. If I recall correctly its all a question of separating the database stuff from the business logic. Having a auto-form from db class would defeat this patern.
And it could get complicated. What about fields you do not want in the form? Like datestamps for creation, modification, last user and stuff like that.

5

Re: Dynamic Forms?

Yeah, that is a good question. I have not thought about those things. I am not that far in my program yet, but eventually I need to find a way to do it.

6

Re: Dynamic Forms?

harri wrote:

Yeah, that is a good question. I have not thought about those things. I am not that far in my program yet, but eventually I need to find a way to do it.

All that being said it should'nt be very difficult to make a class that extends Zend_Form and creates fields from some array (for you data from a query).

7

Re: Dynamic Forms?

So now I'm as far as figuring out how to do this. I'm gonna think out loud and try to explain the situation.

Short info: I use Doctrine, if that would be relevant

The core of the application is to make registration forms or polls, which needs to have the ability to make custom fields/choose the fields needed. Without this capability it would be only one possible hardcoded  (or several different hardcoded) form(s). So the point is to be able to define the fields needed.

I have a model called Form which has a set of attributes (or the table have fields). This is a no-brainer to do with Zend_Form, but a form in my application is not complete without one or more fields (Field model) related to it (the actual fields a user would fill inn to be registered or vote for inn a poll).
A picture to explain the database
Word definitions:
form = form in the application (Form + Field)
fields = fields in a html form (inputs, textarea, etc)
Form = Doctrine based model for meta data about a registration/poll (name,time frame, description, etc)
Field = Doctrine based model that stores info about the fields in the (required, name,  small description, type, etc)


I have wondered (for when an admin create a new form with the application) if it's possible to just make a Zend_Form, that you normally would to pass on data to be persisted, for Form but just leave out the submit element and sneak inn the extra fields (Field) in the view and then send them in the same post as the Zend_Form. And then filter out the fields belonging to the Form model, persist them, thus leaving the Field data left to be persisted.
Would this work? Do you have any other solutions?

Then, for making the form that the user are going to fill out, I could try to do what iznogood suggested in the post above.

What do you guys (or girls?) suggest? I kinda rely on you to help me out here.:)

Just let me know if I need to explain more.:) I'm not sure if explained it clearly enough.

Last edited by harri (2010-06-05 00:11:39)