Best way to create object from a form with lots of fields

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have multiple forms that will create an object.  Basically a energy
efficiency measure object.  The measure object will have a couple of
required properties set but after that it can have 10-20 different fields
that are optional per measure.

How do I account for the different fields that will be
posted from the different forms when I create the measure object?  Should
I create a constructor method with just the required fields as the
parameters and then create some sort of setter method for the optional
fields?  Or do I create a different concrete measure object from an an
abstract measure base class?  I have about 10 different measure objects
that I can think of but really the only difference between them are what
optional fields are posted.

Going forward the other issue is I need these measures to persist across
the pages of the application (sort of like a shopping cart app) and I see
that I can serialize an object in a $_SESSION object but do the fields
that I would set in the setter method also get serialized or do only the
actual properties of the object get set.

I then need to retrieve all these measure objects and send them to a
stored procedure to enter into a database so unfortunately I can't save
the fields as serialized because each field has to be separate to be sent
as the parameters to the stored procedure.

Basically my question is how do people deal with huge forms and fields
easily from a html post?  Any help would be appreciated...thanks...

P.S.  Using ZendFramework1.0RC2 and PHP5.

Re: Best way to create object from a form with lots of fields

Dave wrote:
Quoted text here. Click to load it

Hi Dave,

If the only difference is really what optional fields are to be in the  
form, i would use a single Measure class and ten different layouts for  
the forms.

In any case i do not like the idea of a contructor with the required  
fields as parameters:
1. Validating the field values is typically a task of the object, it is  
kind of hard to ask the object to do that if it has not yet been contructed,
2. If you change your mind and decide that some of the required fields  
should not have been required, or some of the optional fiels are to be  
required, you have to change the constructor, and all the calls that are  
made to it.

A better apoach is to use meta data to declare wheather a field is  
required or optional. When the form is processed you simply let the  
object check if each of the required fields is holding valid data and  
produce user-understandable error feedback if necessary.

Quoted text here. Click to load it

If the system is for a small audience of authenticated users i would  
store the Measure objects in a database table of pending measures and  
give them the user name as a foreign key. Then if a user returns he will  
not have lost all of the measures he has already submitted. If he has  
finished entering measures he should press a button or so to store all  
pending measures in the final database. If successfull this will delete  
his pending measures.

If the system is for a large audience of unauthenticated users, like a  
shopping cart, i would store the pending measures in the session. But I  
would not use serialize on the object itself but on an array with the  
field names as keys and their values as values. That way there will not  
be any errors because of the class not yet being included or being renamed.
Quoted text here. Click to load it

IMHO the framework should offer a way to declare the meta data and take  
care of the conversion and validation of form values.


Henk Verhoeven,

Site Timeline