Mango::Catalyst::Controller::Form - Catalyst controller for form based activity.
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) { ... }; }; };
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.
form_directory
The following configuration options are used directly by this controller:
The form class used to parse/validate forms. The default class is Mango::Form.
__PACKAGE__->config( form_class => 'MyApp::Form' );
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' );
The following method attribute are available:
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 };
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 };
Creates an instance of the current controller and loads all available forms from the appropriate directory.
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.
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:
form
sub foo : Local { $self->form('edit'); ## root/forms/controller/name/edit.yml };
When a form is returns, the following are set automatically:
Gets/sets the form class to be used when loading/validating forms. The default class is Mango::Form.
__PACKAGE__->form_class('MyApp::Form');
Gets/sets the form_directory where forms should be loaded from.
__PACKAGE__->form_directory('/path/to/controllers/forms/');
Returns true if the current form has been submitted, false otherwise.
sub edit : Local { my ($self, $c) = @_; if ($self->submitted) { ... }; };
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}; }; }; };
Mango::Form, Mango::Form::Results
Christopher H. Laco CPAN ID: CLACO claco@chrislaco.com http://today.icantfocus.com/blog/
To install Mango, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mango
CPAN shell
perl -MCPAN -e shell install Mango
For more information on module installation, please visit the detailed CPAN module installation guide.