[html-formfu] new foobar_FORM_SUBMITTED type special-methods

Carl Franks fireartist at gmail.com
Mon Aug 10 14:19:45 GMT 2009


Taking a leaf out of Catalyst::Action::REST's book - I've just checked
in support for special methods that only get called under certain
circumstances.

An example showing how a complicated action method can be broken down into
smaller sections, making it clearer which code will be run, and when.

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

        my $form  = $c->stash->{form};
        my $group = $c->stash->{group};

        $c->detach('/unauthorised') unless $c->user->can_edit( $group );

        if ( $form->submitted_and_valid ) {
            $form->model->update( $group );

            $c->response->redirect( $c->uri_for('/group', $group->id ) );
            return;
        }
        elsif ( !$form->submitted ) {
            $form->model->default_values( $group );
        }
    }

Instead becomes...

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

        $c->detach('/unauthorised') unless $c->user->can_edit(
            $c->stash->{group}
        );
    }

    sub edit_FORM_VALID {
        my ( $self, $c ) = @_;

        my $group = $c->stash->{group};

        $c->stash->{form}->model->update( $group );

        $c->response->redirect( $c->uri_for('/group', $group->id ) );
    }

    sub edit_FORM_NOT_SUBMITTED {
        my ( $self, $c ) = @_;

        $c->stash->{form}->model->default_values(
            $c->stash->{group}
        );
    }


I'm just in the process of changing an application to use this, so may
come across problems or scenarios I've missed - so if anyone else is
interested, please checkout Catalyst-Controller-HTML-FormFu from svn
and let me know how it works for you!

The following method-suffixes are currently supported:

_FORM_VALID

Run when the form has been submitted and has no errors.

_FORM_SUBMITTED

Run when the form has been submitted, regardless of whether or not there was
errors.

_FORM_COMPLETE

For MultiForms, is run if the MultiForm is completed.

_FORM_NOT_VALID

Run when the form has been submitted and there were errors.

 _FORM_NOT_SUBMITTED

Run when the form has not been submitted.

_FORM_NOT_COMPLETE

For MultiForms, is run if the MultiForm is not completed.

_FORM_RENDER

For normal Form base classes, this subroutine is run after any of the
other special methods, unless $form->submitted_and_valid() is true.

For MultiForm base classes, this subroutine is run after any of the other
special methods, unless $multi->complete() is true.



More information about the HTML-FormFu mailing list