|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Zealot
![]() ![]() Join Date: Mar 2004
Location: www.Caracas.ve
Posts: 172
|
How to design an HTML Form class
I'm trying to design an HTML Form class and I need some help with some concepts.
Buttons, for example, are easy to visualize as "objects" but when I create an HTML form, all I really need is for the code (class, function, method, or whatever I wind up using) to write a bunch of HTML. The button has no properties and the only method is writing itself as HTML code. Does this mean that it makes more sense to write the button code as a function as opposed to a class? Or should the button instead of being an object be a method of the Form class? The same applies to all the other form objects such as select, radio buttons and check boxes. BTW, I'm saving the state of radio buttons and other elements in a different object such as a shopping cart. Thanks for your help!
__________________
Denny Schlesinger web services |
|
|
|
|
|
#2 |
|
Twitter: @AnthonySterling
![]() Join Date: Apr 2008
Location: North-East, UK.
Posts: 3,910
|
I would call a button a form element, much like input, label etc...
As such, I would have a Form object which accepted Form_Element objects to compose the form.
__________________
@AnthonySterling: Full-time IT Manager, part-time Freelance PHP Ninja. Also future, drunk, attendee of the PHPNW10 Conference - come say hello! ![]() |
|
|
|
|
|
#3 | |
|
SitePoint Zealot
![]() ![]() Join Date: Mar 2004
Location: www.Caracas.ve
Posts: 172
|
Quote:
SilverBullet, your answer is yes, go for it! Right?
__________________
Denny Schlesinger web services |
|
|
|
|
|
|
#4 | |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Jan 2005
Location: heaven
Posts: 948
|
Quote:
PHP Code:
__________________
Creativity knows no other restraint than the confines of a small mind. - Me Geekly Humor Oh baby! Check out the design patterns on that framework! |
|
|
|
|
|
|
#5 |
|
SitePoint Zealot
![]() ![]() Join Date: Mar 2004
Location: www.Caracas.ve
Posts: 172
|
Thanks for the code sample. I've not structured anything yet, I write mostly procedures and I'm still trying to get my head around OOP.
Looking at your code I think that the issue to solve is nesting a group of tags inside other tags (radio buttons inside a field set, options inside a select), and all these tags inside the form tag in addition to populating the tags with name=value pairs (attributes). The class could be a lot more general than just a form builder. An article I read recently suggested that the most efficient way to pass parameters to an object is to pass a single associative array and that already has the name=value pairs. Just thinking out loud. ![]()
__________________
Denny Schlesinger web services |
|
|
|
|
|
#6 |
|
Programming Team Member
![]() ![]() Join Date: May 2006
Location: Powys, Mid Wales
Posts: 6,734
|
Most of the answers you'll get in the Application Design forum will be OOP-based because that's what Application Design is about.
Not really efficient in terms of the program - Efficient for the programmer, maybe - but not by much. |
|
|
|
|
|
#7 | ||
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Jan 2005
Location: heaven
Posts: 948
|
Quote:
Quote:
__________________
Creativity knows no other restraint than the confines of a small mind. - Me Geekly Humor Oh baby! Check out the design patterns on that framework! |
||
|
|
|
|
|
#8 | ||
|
SitePoint Zealot
![]() ![]() Join Date: Mar 2004
Location: www.Caracas.ve
Posts: 172
|
Quote:
![]() The only production object code I have written is a File class that manages various types of files: mail attachments, uploads, downloads, file storage in MySQL. Quote:
__________________
Denny Schlesinger web services |
||
|
|
|
|
|
#9 |
|
Twitter: @AnthonySterling
![]() Join Date: Apr 2008
Location: North-East, UK.
Posts: 3,910
|
I was thinking something more along these lines...
PHP Code:
__________________
@AnthonySterling: Full-time IT Manager, part-time Freelance PHP Ninja. Also future, drunk, attendee of the PHPNW10 Conference - come say hello! ![]() |
|
|
|
|
|
#10 |
|
Community Advisor
![]() ![]() Join Date: Jun 2004
Location: Copenhagen, Denmark
Posts: 6,048
|
It depends. Making the form element an object, rather than a method, you gain the advantage of polymorphism - Which is really just a type of decoupling. Decouplings are good if they are used in the proper places, but they add unnecessary complexity, if they are placed in the wrong places. In this case, it's pretty common to make the form elements objects, but you might want to start out with a less flexible - simpler - design, and change it when you see the need.
__________________
Introducing pearhub | Konstrukt 2.3.1 | twitter.com/troelskn |
|
|
|
|
|
#11 | |
|
SitePoint Zealot
![]() ![]() Join Date: Mar 2004
Location: www.Caracas.ve
Posts: 172
|
Quote:
I don't see the need to have a class for each form element so I simplified your code a bit. My version has a single Tag class and the constructor is passed the actual tag (input, button, etc.): PHP Code:
Would you be kind enough to explain this line of code PHP Code:
__________________
Denny Schlesinger web services |
|
|
|
|
|
|
#12 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Mar 2007
Location: Czech Republic
Posts: 358
|
It is type hint:
http://www.php.net/manual/en/languag...ypehinting.php |
|
|
|
|
|
#13 |
|
SitePoint Zealot
![]() ![]() Join Date: Mar 2004
Location: www.Caracas.ve
Posts: 172
|
__________________
Denny Schlesinger web services |
|
|
|
|
|
#14 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Jan 2005
Location: heaven
Posts: 948
|
A class for each element because each element separate object. You don't want to over simplify the relation between objects. It will come back to bite you eventually.
__________________
Creativity knows no other restraint than the confines of a small mind. - Me Geekly Humor Oh baby! Check out the design patterns on that framework! |
|
|
|
|
|
#15 |
|
SitePoint Zealot
![]() ![]() Join Date: Mar 2004
Location: www.Caracas.ve
Posts: 172
|
With your help I've built a Tag class that can write a whole HTML page.
PHP Code:
Thanks guys!
__________________
Denny Schlesinger web services |
|
|
|
|
|
#16 | |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Jan 2005
Location: heaven
Posts: 948
|
Quote:
PHP Code:
PHP Code:
__________________
Creativity knows no other restraint than the confines of a small mind. - Me Geekly Humor Oh baby! Check out the design patterns on that framework! |
|
|
|
|
|
|
#17 |
|
SitePoint Zealot
![]() ![]() Join Date: Mar 2004
Location: www.Caracas.ve
Posts: 172
|
Most tags have the same structure, a tag name, zero or more attributes and optional content. I don't see why these would need separate objects. But there are specialized tags, I can think of three: select, radio, and checkbox, that do need their own classes which I plan on building.
__________________
Denny Schlesinger web services |
|
|
|
|
|
#18 | |
|
Twitter: @AnthonySterling
![]() Join Date: Apr 2008
Location: North-East, UK.
Posts: 3,910
|
Quote:
Without rendering the tag, there is no way to determine which form element it represents.
__________________
@AnthonySterling: Full-time IT Manager, part-time Freelance PHP Ninja. Also future, drunk, attendee of the PHPNW10 Conference - come say hello! ![]() |
|
|
|
|
|
|
#19 | ||
|
SitePoint Zealot
![]() ![]() Join Date: Mar 2004
Location: www.Caracas.ve
Posts: 172
|
Quote:
But it does make sense to have a class that assembles the tags to build the page (or the form) Quote:
![]()
__________________
Denny Schlesinger web services |
||
|
|
|
|
|
#20 |
|
Twitter: @AnthonySterling
![]() Join Date: Apr 2008
Location: North-East, UK.
Posts: 3,910
|
Maybe this clears it up a little.
![]() PHP Code:
PHP Code:
__________________
@AnthonySterling: Full-time IT Manager, part-time Freelance PHP Ninja. Also future, drunk, attendee of the PHPNW10 Conference - come say hello! ![]() |
|
|
|
|
|
#21 | |||
|
SitePoint Zealot
![]() ![]() Join Date: Mar 2004
Location: www.Caracas.ve
Posts: 172
|
Quote:
Quote:
Quote:
![]()
__________________
Denny Schlesinger web services |
|||
|
|
|
|
|
#22 | |
|
SitePoint Zealot
![]() ![]() Join Date: Mar 2004
Location: www.Caracas.ve
Posts: 172
|
Quote:
PHP Code:
![]() What's wrong with this? I suppose I can extend the Tag class to create special purpose classes, which is what I was thinking of doing with radio, checkbox, and select.
__________________
Denny Schlesinger web services |
|
|
|
|
|
|
#23 |
|
Twitter: @AnthonySterling
![]() Join Date: Apr 2008
Location: North-East, UK.
Posts: 3,910
|
In my opinion, once you create a generic object that replaces a natural collection of objects, you lose all the benefits of OOP.
For example you wouldn't have a single login form object, you split it down into its base components and create an object to recompile these if necessary. You are already being forced into extending Tag to create the radio, select & checkbox elements, it just seems messy to me. I think you missed my point a little with my example, I was wondering how you would target a specific form element in a programmic manner. As far as I can see, you cannot with your solution.
__________________
@AnthonySterling: Full-time IT Manager, part-time Freelance PHP Ninja. Also future, drunk, attendee of the PHPNW10 Conference - come say hello! ![]() |
|
|
|
|
|
#24 | |
|
SitePoint Zealot
![]() ![]() Join Date: Mar 2004
Location: www.Caracas.ve
Posts: 172
|
Quote:
![]() We have a saying in Spanish: "Neither bald nor with two wigs." How many classes do you want to have? One for each tag? That seems like overkill. I started with a form builder but in fact it uses a lot of tags besides the data input tags. I find myself using div, fieldset, legend, label, br, a, and a few others in forms. According to W3C there are some 90 xhtml tags. Where do you cut off?
__________________
Denny Schlesinger web services |
|
|
|
|
|
|
#25 |
|
SitePoint Enthusiast
![]() Join Date: Aug 2005
Location: Germany
Posts: 25
|
I find it easier to build my forms in HTML and implement them through templates than writing a set of classes instead. My forms (once developed) seldomly change so creating them on the fly through code seems like much work for little benefit for me. And there's always the case where the class does not cover the functionality I need. I had the case where I needed to create dependant drop down select lists - does your class also generate the javascript for managing the dependant option elements? Creating multipage forms through code was also difficult for me, wether you use sessions or "fake" multipage through javascript by hiding form elements.
I do use code though to generate all kinds of lists in forms when the list options are dynamically created - but these classes are more like HTML widgets and do not depend on each other. So from my experience I never found it worth to develop a set of classes that deal with creating forms and form elements - but I still haven't seen it all so I might be complety wrong ![]() |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 05:40.





















Linear Mode
