HTML::FormHandler provides a lot of flexibility and power to your form handling classes.

How FormHandler Works

FormHandler uses form classes that you define. For a sample MyApp you might create a MyApp/Form directory. You can also define custom fields, so you may want a MyApp/Form/Field directory, too.

Here is a simple login form:

   package MyApp::Form::Login;
   use HTML::FormHandler::Moose;
   extends 'HTML::FormHandler';

   has_field 'user' => ( label => 'Username', required => 1 );
   has_field 'password' => ( type => 'Password', required => 1 );
   has_field 'submit' => ( type => 'Submit', value => 'Login' );

You'll want to have some constraints on the password, of course. To do that, you could use a Moose type:

   has_field 'password' => ( type => 'Password', apply => [Password], required => 1 );

Or you could add a form method to perform validation:

   sub validate_password {
      my ( $self$field ) = @_;
      ....perform validation of $field->value ....

or write your own password field:

   package MyApp::Form::Field::Password;
   use HTML::FormHandler::Moose;
   extends 'HTML::FormHandler::Field::Password';

   sub validate {
      my $self = shift;
      ..... perform whatever validation you prefer...

and use it in your form:

   has '+field_name_space' => ( default => 'MyApp::Form::Field' );
   has_field 'password' => ( type => '+Password' );

In a controller you create an instance of your form and process it:

   my $form = MyApp::Form::Login->new;
   return unless $form->process$c->req->params );

You can use your own html or templates, but a simple form can be displayed easily in a template after putting the form in the stash:

    $c->stash( form => $form );
    [% form.render %]

When the form validates - i.e. there are no errors, the database is automatically updated if it is a database form (extended 'HTML::FormHandler::Model::DBIC') or you can get a hash of the values:

    my $value = $form->value;


My tags:
Popular tags:
Powered by Catalyst
Powered by MojoMojo Hosted by Shadowcat - Managed by Nordaaker