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

NAME

Dancer::Plugin::Form - Dancer form handler for Template::Flute template engine

VERSION

Version 0.0061

SYNOPSIS

Display template with checkout form:

    get '/checkout' => sub {
        my $form;

        $form = form('checkout');
        
        template 'checkout', {form => $form};
    };

Retrieve form input from checkout form:

    post '/checkout' => sub {
        my ($form, $values);

        $form = form('checkout');
        $values = $form->values();
    };

Reset form after completion to prevent old data from showing up on new form:

    $form = form('checkout');
    $form->reset;

DESCRIPTION

Dancer::Plugin::Form is used for forms with the Dancer::Template::TemplateFlute templating engine.

Form fields, values and errors are stored into and loaded from the session key form.

METHODS

new

Creates Dancer::Plugin::Form object.

name

Get form name:

    $form->name

action

Set form action:

   $form->action('/checkout');

Get form action:

   $action = $form->action;

fill

Fill form values:

    $form->fill({username => 'racke', email => 'racke@linuxia.de'});
    

values

Get form values as hash reference:

    $values = $form->values;

valid

Determine whether form values are valid:

    $form->valid();

Return values are 1 (valid), 0 (invalid) or undef (unknown).

Set form status to "valid":

    $form->valid(1);

Set form status to "invalid":

    $form->valid(0);

The form status automatically changes to "invalid" when errors method is called with error messages.

errors

Set form errors:

   $form->errors({username => 'Minimum 8 characters',
                  email => 'Invalid email address'});

Get form errors as hash reference:

   $errors = $form->errors;

errors_hashed

Returns form errors as array reference filled with a hash reference for each error.

failure

Indicates form failure by passing form errors.

    $form->failure(errors => {username => 'Minimum 8 characters',
                              email => 'Invalid email address'});

You can also set a route for redirection:

    return $form->failure(errors => {username => 'Minimum 8 characters'},
        route => '/account');

Passing parameters for the redirection URL is also possible:

    return $form->failure(errors => {username => 'Minimum 8 characters'},
        route => '/account',
        params => {layout => 'mobile'});

Please ensure that you validate input submitted by an user before adding them to the params hash.

fields

Set form fields:

    $form->fields([qw/username email password verify/]);

Get form fields:

    $fields = $form->fields;

reset

Reset form information (fields, errors, values, valid) and updates session accordingly.

from_session

Loads form data from session key 'form'. Returns 1 if session contains data for this form, 0 otherwise.

to_session

Saves form name, form fields, form values and form errors into session key 'form'.

AUTHOR

Stefan Hornburg (Racke), <racke at linuxia.de>

BUGS

Please report any bugs or feature requests to bug-dancer-template-templateflute at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dancer-Template-TemplateFlute. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Dancer::Plugin::Form

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2011-2013 Stefan Hornburg (Racke).

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.