views:

57

answers:

3

Hi!

I am building a form generator/builder for Zend Framework. It is for a client, and because the client wants to build forms without going into code, I need a drag&drop interface. The user has to be able to drag&drop predefined Form elements into a container to create a form. This is the easy part, which I've almost solved myself. The harder part, and the question is about the saving of this form. I wish to save this form in a MySQL database. How would I approach this?

I was thinking of:

  • Saving the elements into a database
  • Saving the form into a databse, including the element id's
  • extending the Zend_Form class, so that when a form is requested, it loads all of the elements and creates the needed php code, so that it can be rendered and also populated, if needed for editing.

Does anyone have a better suggestion, or is this the way to go?

Any opinion welcome!

A: 

I was trying to do the same thing some time ago.

Seems that the best option is to save Zend Forms in Zend_Config format (ini or xml, which is simpler to process via JavaScript).

But the generator is not really needed. I just created shortcuts in my IDE (for form elements, validators etc.), so I can easily extend abstract My_Form.

Consider sharing code of your generator, maybe some other programmers may join and help.

takeshin
Thanks for the input, but how does one go about saving something to an ini file and the retrieving it? Just using fwrite, using some specific naming scheme, and then retrieving it? As to the generator, I edited the main question.
Janis Peisenieks
@Janis There are writers, e.g. `Zend_Config_Writer_Ini` or `Zend_Config_Writer_Xml`
takeshin
A: 

I've had to do this a few times now... and it's pretty simple.

  1. Store required info: label, field name, type, options, etc. (you can get fancy with validators/filters if you want) per field.

  2. Dynamically generate the form by iterating over the database-stored fields, using the type / options to create each element.

Adrian Schneider
Thanks for the input. This was the original way I was going to do this. Now there are 3 possible scenarios. Oh... what to do, what to do... :)
Janis Peisenieks
A: 

Most classes in ZF can be serialized, so if your system already takes care of building the form elements presumably you have a Zend_Form object.

Just serialize() it and store it in a text or mediumtext field type. When you load it back in, just unserialize() and you have yourself a functional Zend_Form object.

simonrjones
Ok, This whole caching and serializing of classes is something new to me. So, do I just use it like this: $var=unserialize($serialized_class); $var->render(); ? for an example?
Janis Peisenieks
Most variables can be serialized in PHP, apart from resources to things like an open database connection or a file. If you store the serialized version of your form in the database you should be able to wake it up again (unserialize it) once you load it in from the database. Just call unserialize() on the text string and test to make sure it's a valid Zend_Form class. If it is, you can use it as a normal Zend_Form class and simply echo it in your view to render it. Be aware the text string may be quite long, I tested it a moment ago with a form with 9 elements and the serialized string was 3
simonrjones
Had a few problems adding the comment there, sorry about that. The end of that message should say the serialized form of a form with 9 elements (i.e. form fields) came out at 34,000 characters long.
simonrjones