Page 1 of 2
Alternative to form generation?
Posted: Mon Jul 13, 2009 12:01 pm
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.
Re: Alternative to form generation?
Posted: Mon Jul 13, 2009 12:12 pm
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.
Re: Alternative to form generation?
Posted: Mon Jul 13, 2009 2:52 pm
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>');
}
}
Re: Alternative to form generation?
Posted: Tue Jul 14, 2009 12:32 pm
by alex.barylski
The problem with using jQuery (if I understand correctly) is it does little for accessibility (ie: JS turned off).
Re: Alternative to form generation?
Posted: Tue Jul 14, 2009 1:23 pm
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.
Re: Alternative to form generation?
Posted: Tue Jul 14, 2009 1:42 pm
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).
Re: Alternative to form generation?
Posted: Tue Jul 14, 2009 1:42 pm
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
Re: Alternative to form generation?
Posted: Tue Jul 14, 2009 1:57 pm
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).
Re: Alternative to form generation?
Posted: Tue Jul 14, 2009 2:12 pm
by alex.barylski
lynx
OK...I'm gonna side with Arborint on this one...lynx is really pushing it
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.
Re: Alternative to form generation?
Posted: Tue Jul 14, 2009 2:22 pm
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

Re: Alternative to form generation?
Posted: Tue Jul 14, 2009 5:35 pm
by Christopher
PCSpectra wrote:OK...I'm gonna side with Arborint on this one...lynx is really pushing it

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

Re: Alternative to form generation?
Posted: Tue Jul 14, 2009 5:57 pm
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.
Re: Alternative to form generation?
Posted: Tue Jul 14, 2009 7:51 pm
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.
Re: Alternative to form generation?
Posted: Tue Jul 14, 2009 8:02 pm
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.
Re: Alternative to form generation?
Posted: Tue Jul 14, 2009 8:24 pm
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.