[Catalyst] Some "How Do I ..." questions

Michael Reece mreece at sacbee.com
Tue Apr 26 19:42:46 CEST 2005


On 4/23/05 12:58 PM, "Chisel Wright" <chisel at herlpacker.co.uk> wrote:

> My question is: what's the best way to send some-one through login, then
> once successfully logged in continue their journey onto the original
> request (in this case, /restricted/foo/X).

One way would be for your login form to store the URI in a hidden form
variable.  In mason (ie, Catalyst::View::Mason), it would be something like:

<input type="hidden" name="continue" value="<% $c->http->request->uri %>" />

Then upon successful login, you can $c->redirect( $c->req->{continue} );


> Basically, I want to:
> - know i've got a form submit (deduce add/edit from URL I guess)
> - validate form data
> - insert/update record accordingly.

I am currently using FormValidator and Class::DBI::FromForm.

My base model class defines a formvalidator_profile method:

sub formvalidator_profile {
    return {
        optional => [ shift->columns ],
        missing_optional_valid => 1,
     }
}

Actions for 'add' and 'new' serve up the forms, which submit to do_add and
do_edit.

My CRUD controller then dispatches (via sub process) a request for
/table/do_edit/1 to my _action_do_edit method.

sub _action_do_edit {
    my ( $self, $c ) = @_;
    my $class = $c->stash->{class};

    my $form = $c->form( $class->formvalidator_profile );
    if ($form->has_missing || $form->has_invalid) {
        $c->stash->{form_errors} = $form->msgs;
        $c->stash->{template} = 'edit';
        my $o = $c->stash->{item};
        foreach (keys %{ $c->req->params }) {
            if ($o->find_column( $_ )) {
                $o->{ $_ } = $c->req->params->{ $_ };
            }
        }
        $c->stash->{item} = $o;
        return 0;
    } else {
        $c->stash->{item}->update_from_form( $c->form );

        $c->response->redirect( $c->request->{base} .
            $class->table . '/view/' .  $c->stash->{item}->id );
        return 1;
    }
}

sub _action_do_add {
    my ( $self, $c ) = @_;
    my $class = $c->stash->{class};

    my $form = $c->form( $class->formvalidator_profile );

    if ($form->has_missing || $form->has_invalid) {
        $c->stash->{form_errors} = $form->msgs;
        $c->stash->{template} = 'add';
        return 0;
    } else {
        my $o = $class->create_from_form( $c->form );
        $c->stash->{item} = $o;

        $c->response->redirect( $c->request->{base} .
            $class->table . '/view/' . $o->id );
        return 1;
    }
}

If you need more help figuring how to dispatch to the controller methods, I
can share some more code.  But the way it works in my app, is the default
action forwards to MyApp::C::CRUD, which looks for a MyApp::C::CRUD::Table
controller and uses that if it exists (so each class can override its
_action_foo methods).



-- 
michael reece :: web engineer :: mreece at sacbee.com :: (916)321-1249





More information about the Catalyst mailing list