Forms class

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Forms class

Post by BDKR »

I am sick and tired of creating forms. I suspect a lot of people are too. That said, I am curious what others do/how others handle forms. Just earlier while out sucking down caffine and looking at pretty girls, I started writing out some notes on a class to create form elements. Since I've been home, I started writing the class and testing as I go.

It's all good so far but I am curious what some others are doing.

Please don't dump all your code here. That's not what I'm interested in. Just hearing what some of you are doing to deal with creating forms.

Cheers,
BDKR
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I use objects to create option lists - and extend a base class to accept values from arrays or db results or what have you.

Apart from that, I don't do anything special to create form html templates. Dreamweaver (I know, I know :) ) can be handy to knock something up.

Form processing - scroll down a bit here viewtopic.php?t=15107.

I had an idea for a DataChecker class which I've never really got around to working on.

Basically, a single polymorphic DataChecker object would examine the submitted POST array to clean the data, check required keys are set, alien keys alert, produce messages etc. Each form processor would simply define a bunch of meta data: required keys, the fn used to check a key (intval, regex etc) and anything else required. This gets passed to DataChecker.

A first try out seemed to produce some hefty $meta_data arrays though, which I took as a sign of bad design. Instead, maybe I should extend a base DataChecker. Not sure really.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

I've created my own form-class which is pretty straightfoward. You pass an associative array to it and it returns the form-element.
That keeps it all fairly simple and modular, as, for example, any Javascript can be passed a parameter - thus no need for complicated JS-generation.

The checking is done by JS, that part is autogenerated and saved in a property which can be echoed anywhere in the page.

Some fairly standard checking stuff such as email-validation, select-box text, checkbox-validation etc. is automatically included if necessary.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Good stuff J! Mine doesn't go that far yet. However it's also something that I'm going to be fighting with in the next week. I may be sending you a note. :D

Cheers,
BDKR
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Do :)
jaxn
Forum Commoner
Posts: 55
Joined: Fri Jan 16, 2004 1:50 pm
Location: Nashville, TN

PEAR QuickForm

Post by jaxn »

The PEAR QuickForm class is all you need.

It can create forms from scratch or using one of several templating systems. You can add rules to the form (client side and server side). It handles errors really easily (and they automatically display when a reul is broken). When a form is submitted it is validated. If it passes validation then it runs the code you specify, if not then it re-displays the form with any relevant error messages.

I have used it on some really complex forms that had to be dynamically generated since they have different fields for different accounts/users. It works wonderfully.

-Jackson

P.S. the pear-general mailing list is very helpful with QuickForm issues.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

True, but that's too easy ;)
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: PEAR QuickForm

Post by BDKR »

jaxn wrote:The PEAR QuickForm class is all you need.

It can create forms from scratch or using one of several templating systems. You can add rules to the form (client side and server side). It handles errors really easily (and they automatically display when a reul is broken). When a form is submitted it is validated. If it passes validation then it runs the code you specify, if not then it re-displays the form with any relevant error messages.

I have used it on some really complex forms that had to be dynamically generated since they have different fields for different accounts/users. It works wonderfully.

-Jackson

P.S. the pear-general mailing list is very helpful with QuickForm issues.
I decided a long time ago to stay away from PEAR. I'll roll my own. :?
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

Wow .. awesome Jackson! Thanx.

For Robin and Patrick:
What's the problem with using others libraries?
It allows you to progress further by not wasting time doing something that's been done. Not only that, it's been designed well, and tested and used by the community, so bugs would be caught quickly.

Why you would want to waste time and effort doing something that's been done and is available to you is beyond me, when you could spend time working on something else and be productive instead.
jaxn
Forum Commoner
Posts: 55
Joined: Fri Jan 16, 2004 1:50 pm
Location: Nashville, TN

Post by jaxn »

Here here lazy_yogi.

I guess we know who the "hackers" are: http://www.catb.org/hacker-emblem/

-Jackson
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

lazy_yogi wrote: What's the problem with using others libraries?
It allows you to progress further by not wasting time doing something that's been done. Not only that, it's been designed well, and tested and used by the community, so bugs would be caught quickly.

Why you would want to waste time and effort doing something that's been done and is available to you is beyond me, when you could spend time working on something else and be productive instead.
That's a fair point: but where is the quality control? Eclipse & a small number of other libraries I would trust based on their reputation - and the fact that it's very obviously well-written - but how often do you see work of that standard?

Maybe I've got an unreasonable prejudice against PEAR but I don't expect to find usable classes there.
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

McGruff wrote:That's a fair point: but where is the quality control? Eclipse & a small number of other libraries I would trust based on their reputation - and the fact that it's very obviously well-written - but how often do you see work of that standard?
It's a matter of finding it. And if you're working for a company, it's often worth paying for it since if it's from a company, they would (hopefully) have put time and money into it to make sure it a rigid and well designed and tested product. There's no point doing something that's been done. Might as well spend your time on developing new products that require customisation - as is not the case here
McGruff wrote:Maybe I've got an unreasonable prejudice against PEAR but I don't expect to find usable classes there.
Why is that ?


--
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

It's been a while since I looked at PEAR and I didn't go through it all. It does have a bit of a reputation of being bloated. Perhaps there is some good stuff in there.

OOP lets you model problems in any way you like (provided that the basic components are lean, mean and cleanly separate). I'd rather work out my own analysis instead of following a structure which may or may not be very good or which just doesn't suit the way I like to think.

Any third party classes would have to work within my own framework - but it takes time to figure that out. I'd rather spend the time programming rather than analysing other people's code.
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

Sometimes bloat is necessary to give full functionality
Tho if its badly written that can contribute to bloat.
If its the first that's fine. The second - not so fine =P

I'd say that if it's well implemented and tested, and with a nice easy (and hopefully generic) interface, then use someone elses

Don't write your own unless there's a reason
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

I have nothing against using other's libraries - I am using a couple, depending on the application I am developing.

Regarding PEAR: I was looking for a template class quite some time back and had a look at every singe PEAR template package (and yes, even Smarty). Each and every one of them was way too cumbersome and bloated - so I decdided to write my own. It's about 120 lines of code - slim, simple and very easy and straightforward. Yes, it took more time, but it was more fun and I've learned a great deal in the process.

What I found with PEAR in particular (that was some time ago), was that the classes tried to do too much which shifted the focus from what the class was actually meant to do, while the API wasn't clearly thought out at all. Hence the functionality and code was bloated, complicated.
Post Reply