[Catalyst] Recommend methods for form handling

Ian Docherty catalyst at iandocherty.com
Wed Jan 17 21:07:24 GMT 2007


Hartmaier Alexander wrote:
> Hi Jim!
>
> I don't recommend the use of FillInForm, I dropped it finally two weeks ago when I had time to refactor my Catalyst apps that used it.
> Now I handle forms by putting the data they should show on the stash with $c->stash->{form_data}->{fieldname} = 'value';
> I have to private actions to do this, one _from_db and one _from_params.
> Their names should be self-explanatory.
> E.g. the edit action forwards to the _from_db method on first load of the form which is detected by checking for method POST and the value of the submit button, else the _from_params.
>
> With this I don't have to overwrite the cgi params and have full control of what my form should display.
>
> Those values are used in my templates like value="[% form_data.fieldname %]".
>
> You can look at FormBuilder which should be pretty good from what I've heard.
> I'll wait for Reaction and invest some time in learning how to use it when it's done.
>
> Ah, and I use Plugin::FormValidator for the form validation. A private do_form method does this and is used for both add and edit methods.
>
> -Alex
>   
I tried this approach myself some time ago, before I moved to FillInForm 
but I found in cumbersome. I would like to you say what you did not like 
about FillInForm and why you don't recommend it?

The trouble with your _from_db and your _from_params approach (which is 
similar to the approach I originally used) is that you have to write a 
long sequence of copies from one place to another and to keep track of 
all the things you have to copy, adding new ones when you add form 
elements. It breaks the DRY approach since you now have similar code in 
at least two or three places. (from the db to the stash, from the params 
to the stash, from the stash to the db).

What I especially like about using FillInForm is that if I have a form 
that edits (say) a user, I simply pass the user object on the stash. The 
template then refers to [% user.firstname %], [% user.lastname %] etc. 
and FillInForm does the rest when the form is re-submitted. It makes my 
controllers so much simpler.

Regards
Ian Docherty (IcyDee)




More information about the Catalyst mailing list