Alternative to form generation?

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
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Alternative to form generation?

Post by allspiritseve »

I've never really liked the idea of form generation. I generally don't have a problem writing forms by hand.

That being said... what annoys me the most about hand generated forms is how ugly everything gets when you add in error messages, default values, and saved values from a previous request. What I would like is some jQuery-type way to select a specific element and modify it in some way. So maybe I could call something like this:

Code: Select all

foreach($errors as $name => $error) {
   $elements->get($name)->addClass('error');
   // or
   $elements->get("input [name=$name]")->addClass('error');
}
foreach($values as $name => $value) {
   $elements->get($name)->attr('value',$value);
   // or
   $elements->get("input [name=$name]")->setValue($value);
}
 
I haven't thought through it too much, but it would save me some of the monotony of writing forms.
Last edited by allspiritseve on Mon Jul 13, 2009 12:14 pm, edited 1 time in total.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Alternative to form generation?

Post by alex.barylski »

I dislike form generation for several reasons:

1. Loss of control over every element, etc.
2. Templates are consistent with the rest of the system

I don't use a table generating class for my record listings, etc.

I use templates (but obviously they are flawed as well as you have pointed out).

One solution I have considered is using the DOM to set field values and basically just use templates to generate the form and initialize drop downs with contents from the DB or whever.

Something like jQuery would work in a very similar fashion (XSLT?) -- you would first run the template through it's phase then pass the resulting HTML to the DOM and manipulate the form elements, like setting a checkbox to checked, which is quite messy in PHP/HTML logic.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Alternative to form generation?

Post by Darhazer »

miphpf have interesting approach to forms (don't look at documentation, as it's outdated)

Basically there is a form object and form widgets... those widget usually contain only the value of the form field... the field itself is typed in the HTML template, so it looks like:

Code: Select all

<input type="text" name="SomeFiled" value="%%SOMEFIELD%%" />
And of course there are validators
So basically the flow is:
Data from the model -> form -> widgets get data from form -> submit -> set submitted data to form -> validation error -> widgets get data from form (that's the submitted data)
And for displaying the error messages, we are using a jQuery script, that attach each message to each field...

Code: Select all

this.showErrors = function(errors)
    {
        for (var property in errors) {
            $('[@name=' + property + ']').after('<div class="fieldError">^-- ' + errors[property] + '!</div>');
        }
    }
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Alternative to form generation?

Post by alex.barylski »

The problem with using jQuery (if I understand correctly) is it does little for accessibility (ie: JS turned off).
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Alternative to form generation?

Post by Christopher »

PCSpectra wrote:The problem with using jQuery (if I understand correctly) is it does little for accessibility (ie: JS turned off).
I always hear this. But has anyone every had the problem that someone using one of their sites had Javascript turned off? Maybe ten years ago. I think this is a non-problem that won't die.
(#10850)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Alternative to form generation?

Post by Weirdan »

allspiritseve wrote:What I would like is some jQuery-type way to select a specific element and modify it in some way.
You could use Zend_Dom_Query (http://framework.zend.com/manual/en/zend.dom.query.html).
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Alternative to form generation?

Post by alex.barylski »

I always hear this. But has anyone every had the problem that someone using one of their sites had Javascript turned off?
Mobile devices like my BlackBerry don't have JS enabled by default...it's really annoying.

My main development machine, which I usually use for browsing, is running IE8 and I deliberately disbale JS, ActiveX, etc because I don't want popups, malware, etc getting on the machine I use for banking.

Sure I'm a power user and can quickly figure out whether I need to use another browser, computer, etc, but the fact remains, there are situations where JS is disabled.

Not to mention, IMHO when you build an framework/application to account for all these little caveats:

1. Accessibility
2. Security
3. Scalability
4. Standards
6. Usability

And so on and so forth, it tends to push your design in a cleaner, simplified direction.

I look at it like this, competition is a b*tch, so if competitor X solves all the same problems my own product does, would it not make sense to have that one additional (undisputable) asset. Whether it be your application validates (which technically does little but IMHO says a lot) or works without javascript, etc.

Cheers,
Alex
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Alternative to form generation?

Post by Weirdan »

arborint wrote:I always hear this. But has anyone every had the problem that someone using one of their sites had Javascript turned off?
I had this problem myself, using lynx (or was that links, can't quite remember) and builtin browser in Siemens M55 (meaning the browser released 2 years after everyone's 'all-time favorite' IE6).
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Alternative to form generation?

Post by alex.barylski »

lynx
OK...I'm gonna side with Arborint on this one...lynx is really pushing it :P

The blackberry though, it's a very common device for business and although I'm sure it's used for email more than browsing, I hate not being able to login to my fav sites because it requires cookies or javascript ot be enabled.
User avatar
Jammerious
Forum Commoner
Posts: 59
Joined: Sat Jun 27, 2009 11:30 am
Location: Slovenia (EU)

Re: Alternative to form generation?

Post by Jammerious »

Interesting, was just going to check on Google Analytics and javascript capabilities are not noted.

As for form generation, for now I like the idea and I'm currently making the Form classes with an OO approach. The model is more defined by the business logic instead of the programming procedure.
This is a work in progress and I might hit a bump here and there because I am not very experienced =) I hope at least the statechart is valid 8O
Image Image
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Alternative to form generation?

Post by Christopher »

PCSpectra wrote:OK...I'm gonna side with Arborint on this one...lynx is really pushing it :P
There is a text based browser that supports javascript, but I can't remember what it is called. I also use lynx on occasion to download software onto servers.
PCSpectra wrote:The blackberry though, it's a very common device for business and although I'm sure it's used for email more than browsing, I hate not being able to login to my fav sites because it requires cookies or javascript ot be enabled.
I think this is an example of a range of new devices you don't YET support Javascript.

You also might want to try this on your Blackberry:

Browser->Options->Browser Configuration->Scroll down->Enable "Support JavaScript"->Save Options

:)
(#10850)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Alternative to form generation?

Post by Eran »

Client-side error messages are nice from a usability standpoint (alerts immediately), but they should be considered progressive enhancement only. Server side validation should always be performed as well, to allow javascript-less users to still use the form and also to prevent advanced users / non-browser visitors (ie robots) from abusing the form.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Alternative to form generation?

Post by Christopher »

I agree. You still always have to do validation on the server side. That is one reason that it is nice to have validation in the Model (something pytrin taught me ;)).

The View is where you treat the user like a friend. The Model is where you treat the user like an enemy.
(#10850)
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Alternative to form generation?

Post by allspiritseve »

I think everyone agrees validation needs to be done server side. But what about feedback for the user? Using javascript won't compromise your security, but usability and accessibility will be sorely limited if all feedback is done through js and javascript is disabled for whatever reason.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Alternative to form generation?

Post by Christopher »

No. I really like the idea of just delivering JSON to forms and letting them do nice things in the browser (lynx and blackberry users be damned). What is the most standard way to use jQuery to populate, validate and show error messages for forms? Especially Ajax forms.
(#10850)
Post Reply