[Catalyst] best practices for handling forms?
Alexander Hartmaier
alexander.hartmaier at t-systems.at
Wed Sep 29 22:24:35 GMT 2010
Take a look at HFH, especially this pod:
http://search.cpan.org/~gshank/HTML-FormHandler-0.32002/lib/HTML/FormHandler/Manual/Cookbook.pod
I tried both FormFu and FormHandler and the later is much cleaner code
and also a lot faster although it uses Moose.
--
Best regards, Alex
Am Montag, den 20.09.2010, 23:48 +0200 schrieb E R:
> Hi,
>
> I am curious what everyone thinks as being the best practices for
> handling forms in Catalyst. Are there any Catalyst applications you
> have run across which are good examples of how to use Catalyst's
> features to handle forms?
>
> To illustrate what I am getting at, below is typical Rails (v2)
> controller code which implements updating the attributes of an object:
>
> def edit
> @book = Book.find(params[:id])
> @subjects = Subject.find(:all)
> end
> def update
> @book = Book.find(params[:id])
> if @book.update_attributes(params[:book])
> flash[:notice] = 'Book successfully updated.'
> redirect_to :action => 'show', :id => @book
> else
> @subjects = Subject.find(:all)
> render :action => 'edit'
> end
> end
>
> In Catalyst, this would be appear something like (and please correct
> me if I have made any errors here):
>
> sub edit :Args(1) {
> my ($self, $c, $id) = @_;
> ... set up $c->stash for template 'edit' ...
> # no need to set $c->stash->{template} - will be set from the current action
> }
>
> sub update :Args(1) {
> my ($self, $c, $id) = @_;
> ...process form...
> if (form is valid) {
> ...perform updates...
> $c->flash->{notice} = 'Book successfully updated.';
> $c->res->redirect('show', $id);
> } else {
> ... set up $c->stash for 'edit' template ...
> $c->stash->{template} = 'edit';
> }
> }
>
> Any comments on this architecture? Is there a better way? My main problems are:
>
> 1. The code ... set up $c->stash for 'edit' template ... is duplicated
> in both edit and update (which is also true for the Rails code).
>
> 2. Having the template name defaulted from the current action is nice,
> but that means we have to explicitly set it in the update method. Is
> it better to always explicitly set the template name in a controller
> method? Then update could perform a $c->detach('edit', $id) or would
> you use $c->go('edit', $id)?
>
> Thanks,
> ER
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
T-Systems Austria GesmbH Rennweg 97-99, 1030 Wien
Handelsgericht Wien, FN 79340b
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
Notice: This e-mail contains information that is confidential and may be privileged.
If you are not the intended recipient, please notify the sender and then
delete this e-mail immediately.
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*
More information about the Catalyst
mailing list