[Catalyst] Form handling, urls and web design

Bill Moseley moseley at hank.org
Tue Jan 24 00:47:40 CET 2006


On Mon, Jan 23, 2006 at 09:47:17PM +0000, Alex Kavanagh wrote:
> At the moment, I have two URLs to deal with this:
> 
> /manage/users             - provide the list of users
> /manage/users/<username>  - deal with a particular user.
> 
> I have a controller called Manage::Users.pm which appropriately looks
> after the /manage/users url fragment.
> 
> Currently, I have two main functions which are called by the Catalyst
> framework:
> 
> sub default : Private {
> }
> 
> looks after the list of users
> 
> and
> 
> sub user : LocalRegex( '(\w+)$' ) {  # ';
> }
> 
> to handle the user.

I stay away from default.  Use an empty path instead.

How about:

    # Display a list of users for /manage/users
    sub list : Path {
        my ( $self, $c ) = @;


    # Edit, Create, Update a user
    sub edit : Local {
        my ( $self, $c, $user ) = @_;


In edit() if $user is passed in then fetch the user and display the
form populated.  If no user is passed in show a blank form for
create.  FillInForm works wonders here.


I use the generic "list" and "edit" names.  Then templates end up
consistent: 

    $template_root/manage/users/list.tt
    $template_root/manage/users/edit.tt

    $template_root/manage/stuff/list.tt
    $template_root/manage/stuff/edit.tt


I use the same "edit" action for edit/create/update.  So my edit
actions look like:

    sub edit : Local {
        my ( $self, $c, $id ) = @_;

        my $form = $c->stash->{form} = App::Form::Admin::Organization->new( $id );

        return $c->internal_redirect('list', { message => 'Invalid Organization Selected' } )
            unless $form;

        # Now validate and update
        $form->update_from_form( $c->req->parameters )
            if $c->form_posted;
    }


That shows the form again after update.  Otherwise, I'd do a
redirect if update_from_form returns true.  If it returns false then
the form didn't validate and it needs to be redrawn.

> Thus as far as the web-user is concerned all they every see is
> 
> http://..../manage/users
> 
> or
> 
> http://..../manage/users/barty

What if barty is not unique or decides on a new name? ;)


-- 
Bill Moseley
moseley at hank.org




More information about the Catalyst mailing list