The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mango::Catalyst::Controller::Form - Catalyst controller for form based activity.

SYNOPSIS

    package MyApp::Controller::Stuff;
    use base qw/Mango::Catalyst:Controller::Form/;

    sub edit : Local {
        my ($self, $c) = @_;
        if ($self->submitted) {
            my $results = $self->validate;
            if ($results->success) {
                ...
            };
        };
    };

DESCRIPTION

Mango::Catalyst::Controller::Form is a base Catalyst controller that automatically loads forms based on the current class/action using Mango::Form.

By default, this controller loads all forms from a directory named after the current controller in the root/forms directory and assigns them to the actions in that controller matching the name of the form file themselves.

For example:

    ## controller
    MyApp::Controller::Foo::Bar;

    ## directory/files
    root/forms/foo/bar/create.yml
    root/forms/foo/bar/edit.yml

    ## actions
    sub create : Local {
        $self->form; ## create.yml form
    };

    sub edit : Local {
        $self->form; ## edit.yml form
    };

If you would like to load forms from a different directory, specify that directory using the form_directory configuration option below.

CONFIGURATION

The following configuration options are used directly by this controller:

form_class

The form class used to parse/validate forms. The default class is Mango::Form.

    __PACKAGE__->config(
        form_class => 'MyApp::Form'
    );
form_directory

The directory to load forms from. If no directory is specified, forms in the current class2prefix are loaded instead.

    __PACKAGE__->config(
        form_directory => '/path/to/this/classes/forms'
    );

ATTRIBUTES

The following method attribute are available:

Form

Arguments: $name

Set the name of the form to use for the current method/action.

    sub create : Form('myform') Local {
        my $self = shift;
        my $form = $self->form;  # returns myform.yml form
    };

FormFile

Arguments: $file

Sets the file name of the form to use for the current method/action.

    sub create : FormFile('/path/to/myform.yml') Local {
        my $self = shift;
        my $form = $self->form;  # returns myform.yml form
    };

METHODS

COMPONENT

Creates an instance of the current controller and loads all available forms from the appropriate directory.

ACCEPT_CONTEXT

Accepts the current context and stores a weakened reference to it. This is just a hack so $self->form can access context without having to call $c->forward('form') all the time.

form

Arguments: $name

Gets the form for the current action.

    sub edit : Local {
        $self->form; ## root/forms/controller/name/edit.yml
    };

If you wish to work with a specific form, simply pass the name of that file, without the .yml extension, to form:

    sub foo : Local {
        $self->form('edit'); ## root/forms/controller/name/edit.yml
    };

When a form is returns, the following are set automatically:

params are set to $c->request
action is set to $c->action
localizer is set to $c->localize

form_class

Arguments: $class

Gets/sets the form class to be used when loading/validating forms. The default class is Mango::Form.

    __PACKAGE__->form_class('MyApp::Form');

form_directory

Arguments: $directory

Gets/sets the form_directory where forms should be loaded from.

    __PACKAGE__->form_directory('/path/to/controllers/forms/');

submitted

Returns true if the current form has been submitted, false otherwise.

    sub edit : Local {
        my ($self, $c) = @_;
        if ($self->submitted) {
            ...
        };
    };

validate

Returns a Mango::Results object containing the status of the current form validation.

    sub edit : Local {
        my ($self, $c) = @_;
        if ($self->submitted) {
            my $results = $self->validate;
            if ($results->success) {
                ...
            } else {
                print @{$results->errors};
            };
        };
    };

SEE ALSO

Mango::Form, Mango::Form::Results

AUTHOR

    Christopher H. Laco
    CPAN ID: CLACO
    claco@chrislaco.com
    http://today.icantfocus.com/blog/