CGI-Application-Plugin-FormState

 view release on metacpan or  search on metacpan

lib/CGI/Application/Plugin/FormState.pm  view on Meta::CPAN


package CGI::Application::Plugin::FormState;

use warnings;
use strict;

use CGI::Application;
use CGI::Session::ID::md5;
use CGI::Application::Plugin::Session;
use vars qw(@ISA @EXPORT);


use Carp;
use Scalar::Util qw(weaken isweak);

use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(form_state);

our $CGIAPP_Namespace     = '__CAP_FORM_STATE';
my  $Default_Expires      = '2d';
my  $Default_Storage_Name = 'cap_form_state';

sub import {
    my $caller = scalar(caller);
    if ($caller->can('add_callback')) {
        $caller->add_callback('load_tmpl', \&_add_form_state_id_to_tmpl);
    }
    else {
        croak "CAP::FormState: Calling package ($caller) is not a CGI::Application module so cannot install load_tmpl hooks.  If you are using \@ISA instead of 'use base', make sure it is in a BEGIN { } block, and make sure these statements appear be...
    }
    goto &Exporter::import;
}

=head1 NAME

CGI::Application::Plugin::FormState - Store Form State without Hidden Fields

=head1 VERSION

Version 0.12

=cut

our $VERSION = '0.12';

=head1 SYNOPSIS

FormState is just a temporary stash that you can use for storing and
retrieving private parameters in your multi-page form.

    use CGI::Application::Plugin::FormState;

    my $form = <<EOF;
       <form action="app.cgi">
       <input type="hidden" name="run_mode" value="form_process_runmode">
       <input type="hidden" name="cap_form_state" value="<tmpl_var cap_form_state>">
       ...
       </form>
    EOF

    sub form_display_runmode {
        my $self = shift;

        # Store some parameters
        $self->form_state->param('name'       => 'Road Runner');
        $self->form_state->param('occupation' => 'Having Fun');

        my $t = $self->load_tmpl(scalarref => \$form);
        return $t->output;

    }

    sub form_process_runmode {

lib/CGI/Application/Plugin/FormState.pm  view on Meta::CPAN

it is as simple as:

    $self->redirect($self->link('/app.cgi', 'rm' => 'list', 'cap_form_state' => $self->form_state->id));

Or, in the case of a link to the currently running app:

    $self->redirect($self->self_link('rm' => 'list', 'cap_form_state' => $self->form_state->id));


=head1 IMPLEMENTATION

When you call C<< $self->form_state >> for the first time, a top-level
key is created in the user's session.  This key contains a random,
hard-to-guess element.  It might look something like:

   form_state_cap_form_state_84eb13cfed01764d9c401219faa56d53

All data you place in the form state with C<param> is stored in the
user's session under this key.

You pass the name of this key on to the next instance of your
application by means of a hidden field in your form:

    <input type="hidden" name="cap_form_state" value="<tmpl_var cap_form_state>">

You manually put this hidden field in your template.  The template
parameter C<cap_form_state> is automatically added to your template
parameters via the C<load_tmpl> hook.  It contains the random,
hard-to-guess portion (e.g. C<84eb13cfed01764d9c401219faa56d53>).  When
the template is filled, the hidden field will look something like this:

    <input type="hidden" name="cap_form_state" value="84eb13cfed01764d9c401219faa56d53">

Since all values are stored on the server in the user's session, the
user can't tamper with any of them.

To keep old form_data from cluttering up the user's session, the system
uses L<CGI::Session>'s C<expire> feature to expire old form state keys
after a reasonable amount of time has passed (2 days by default).

You can manually delete a form state storage by calling:

    $self->form_state->delete;

=cut

sub _new {
    my ($class, $webapp) = @_;

    my $self = {
        '__CGIAPP_OBJ'   => $webapp,
        '__STORAGE_NAME' => undef,
        '__STORAGE_HASH' => undef,
        '__STORAGE_KEY'  => undef,
        '__EXPIRES'      => undef,
        '__CONFIGURED'   => undef,
    };

    # Force reference to CGI::Application object to be weak to avoid
    # circular references
    weaken($self->{'__CGIAPP_OBJ'});

    return bless $self, $class;
}

sub form_state {
    my ($self) = @_;

    # It's possible that in the future we will allow named configs, e.g.
    #    $self->form_state('foo')->param('bar);

    if (not exists $self->{$CGIAPP_Namespace}->{'__DEFAULT_CONFIG'}) {
        $self->{$CGIAPP_Namespace}->{'__DEFAULT_CONFIG'} = __PACKAGE__->_new($self);
    }
    return $self->{$CGIAPP_Namespace}->{'__DEFAULT_CONFIG'};
}

=head1 METHODS

=over 4

=item config(%options)

Sets defaults for the plugin.

B<Calling config is purely optional, since the defaults should be fine most purposes.>

    $self->form_state->config('name' => 'storage_names', 'expires' => '3d')

The following options are allowed:

=over 4

=item name

Sets the name of the default form state storage.  This name is used for
the key in the user's session, for the name of hidden form field, and
the template parameter used to fill the hidden form field.  So if you
set the C<name> to C<foo>:

    $self->form_state_config('name' => 'foo');

then the hidden field in your template should look like this:

    <input type="hidden" name="foo" value="<tmpl_var foo>">

and the key in the user's session would look something like this:

   form_state_foo_84eb13cfed01764d9c401219faa56d53

=item expires

Indicates when form state storage keys should expire and disappear from
the user's session.  Uses the same format as L<CGI::Session>'s
C<expire>.  Defaults to 2 days (C<'2d'>).  To cancel expiration and make
the form state last as long as the user's session does, use:

    $self->form_state_config('expires' => 0);

=back



( run in 1.693 second using v1.01-cache-2.11-cpan-39bf76dae61 )