.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "CGI::Application::Plugin::FormState 3pm" .TH CGI::Application::Plugin::FormState 3pm "2022-06-09" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" CGI::Application::Plugin::FormState \- Store Form State without Hidden Fields .SH "VERSION" .IX Header "VERSION" Version 0.12 .SH "SYNOPSIS" .IX Header "SYNOPSIS" FormState is just a temporary stash that you can use for storing and retrieving private parameters in your multi-page form. .PP .Vb 1 \& use CGI::Application::Plugin::FormState; \& \& my $form = < \& \& \& ... \& \& EOF \& \& sub form_display_runmode { \& my $self = shift; \& \& # Store some parameters \& $self\->form_state\->param(\*(Aqname\*(Aq => \*(AqRoad Runner\*(Aq); \& $self\->form_state\->param(\*(Aqoccupation\*(Aq => \*(AqHaving Fun\*(Aq); \& \& my $t = $self\->load_tmpl(scalarref => \e$form); \& return $t\->output; \& \& } \& \& sub form_process_runmode { \& my $self = shift; \& \& # Retrieve some parameters \& print $self\->form_state\->param(\*(Aqname\*(Aq); # \*(AqRoad Runner\*(Aq \& print $self\->form_state\->param(\*(Aqoccupation\*(Aq); # \*(AqHaving Fun\*(Aq \& } .Ve .SH "EXAMPLE" .IX Header "EXAMPLE" This is a more complete example, using CGI::Application::Plugin::ValidateRM. .PP .Vb 3 \& use CGI::Application::Plugin::Session; \& use CGI::Application::Plugin::FormState; \& use CGI::Application::Plugin::ValidateRM; \& \& my $form = < \& \& \& ... \& \& EOF \& \& sub my_form_display { \& my $self = shift; \& my $errs = shift; \& my $t = $self\->load_tmpl(scalarref => \e$form); \& \& # Stash some data into it \& $self\->form_state\->param(\*(Aqname\*(Aq => \*(AqWile E. Coyote\*(Aq); \& $self\->form_state\->param(\*(Aqoccupation\*(Aq => \*(AqMining Engineer\*(Aq); \& \& # Normal ValidateRM error handling \& $t\->param($errs) if $errs; \& return $t\->output; \& } \& \& sub my_form_process { \& my $self; \& \& # Normal ValidateRM validation \& my ($results, $err_page) = $self\->check_rm(\*(Aqmy_form_display\*(Aq,\*(Aq_my_form_profile\*(Aq); \& return $err_page if $err_page; \& \& # The data from the submitted form \& my $params = $self\->dfv_results; \& \& $params\->{\*(Aqname\*(Aq} = $self\->form_state\->param(\*(Aqname\*(Aq); # \*(AqWile E. Coyote\*(Aq \& $params\->{\*(Aqoccupation\*(Aq} = $self\->form_state\->param(\*(Aqoccupation\*(Aq); # \*(AqMining Engineer\*(Aq \& \& \& # Now do something interesting with $params \& # ... \& \& \& my $t = $self\->load_tmpl(\*(Aqsuccess.html\*(Aq); \& return $t\->output; \& } \& \& # Standard ValiateRM profile \& sub _my_form_profile { \& return { \& required => \*(Aqemail\*(Aq, \& msgs => { \& any_errors => \*(Aqsome_errors\*(Aq, \& prefix => \*(Aqerr_\*(Aq, \& }, \& }; \& } .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\f(CW\*(C`CGI::Application::Plugin::FormState\*(C'\fR provides a temporary storage area within the user's session for storing form-related data. .PP The main use of this is for multi-page forms. Instead of using hidden fields to store data related to the form, you store and retrieve values from the form state. .PP In the first instance of your app: .PP .Vb 2 \& $self\->form_state\->param(\*(Aqsome_name\*(Aq => \*(Aqsome_value\*(Aq); \& $self\->form_state\->param(\*(Aqsome_other_name\*(Aq => \*(Aqsome_other_value\*(Aq); .Ve .PP And later, in a different instance of your app: .PP .Vb 2 \& $val1 = $self\->form_state\->param(\*(Aqsome_name\*(Aq); \& $val2 = $self\->form_state\->param(\*(Aqsome_other_name\*(Aq); .Ve .PP To connect the first instance and the second, you put a single hidden field in your template: .PP .Vb 1 \& .Ve .PP You don't have to worry about creating the template param \&\f(CW\*(C`cap_form_state\*(C'\fR; it is added automatically to your template parameters via the \f(CW\*(C`load_tmpl\*(C'\fR hook. .PP If you want to use a parameter other than \f(CW\*(C`cap_form_state\*(C'\fR you can do so via the \f(CW\*(C`name\*(C'\fR parameter to \f(CW\*(C`form_state\-\*(C'\fRconfig>. .PP If you're skeptical about whether all this abstraction is a good idea, see \*(L"\s-1MOTIVATION\*(R"\s0, below. .SH "PRESERVING FORM STATE ACROSS REDIRECTS" .IX Header "PRESERVING FORM STATE ACROSS REDIRECTS" You can include the form_state hash in a link: .PP .Vb 1 \& my $link = \*(Aq/app.cgi?rm=list&cap_form_state=\*(Aq . $self\->form_state\->id; .Ve .PP If you use CGI::Application::Plugin::Redirect, you can easily create redirect this way: .PP .Vb 1 \& $self\->redirect(\*(Aq/app.cgi?rm=list&cap_form_state=\*(Aq . $self\->form_state\->id); .Ve .PP If you also use CGI::Application::Plugin::LinkIntegrity it is as simple as: .PP .Vb 1 \& $self\->redirect($self\->link(\*(Aq/app.cgi\*(Aq, \*(Aqrm\*(Aq => \*(Aqlist\*(Aq, \*(Aqcap_form_state\*(Aq => $self\->form_state\->id)); .Ve .PP Or, in the case of a link to the currently running app: .PP .Vb 1 \& $self\->redirect($self\->self_link(\*(Aqrm\*(Aq => \*(Aqlist\*(Aq, \*(Aqcap_form_state\*(Aq => $self\->form_state\->id)); .Ve .SH "IMPLEMENTATION" .IX Header "IMPLEMENTATION" When you call \f(CW\*(C`$self\->form_state\*(C'\fR 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: .PP .Vb 1 \& form_state_cap_form_state_84eb13cfed01764d9c401219faa56d53 .Ve .PP All data you place in the form state with \f(CW\*(C`param\*(C'\fR is stored in the user's session under this key. .PP You pass the name of this key on to the next instance of your application by means of a hidden field in your form: .PP .Vb 1 \& .Ve .PP You manually put this hidden field in your template. The template parameter \f(CW\*(C`cap_form_state\*(C'\fR is automatically added to your template parameters via the \f(CW\*(C`load_tmpl\*(C'\fR hook. It contains the random, hard-to-guess portion (e.g. \f(CW\*(C`84eb13cfed01764d9c401219faa56d53\*(C'\fR). When the template is filled, the hidden field will look something like this: .PP .Vb 1 \& .Ve .PP Since all values are stored on the server in the user's session, the user can't tamper with any of them. .PP To keep old form_data from cluttering up the user's session, the system uses CGI::Session's \f(CW\*(C`expire\*(C'\fR feature to expire old form state keys after a reasonable amount of time has passed (2 days by default). .PP You can manually delete a form state storage by calling: .PP .Vb 1 \& $self\->form_state\->delete; .Ve .SH "METHODS" .IX Header "METHODS" .IP "config(%options)" 4 .IX Item "config(%options)" Sets defaults for the plugin. .Sp \&\fBCalling config is purely optional, since the defaults should be fine most purposes.\fR .Sp .Vb 1 \& $self\->form_state\->config(\*(Aqname\*(Aq => \*(Aqstorage_names\*(Aq, \*(Aqexpires\*(Aq => \*(Aq3d\*(Aq) .Ve .Sp The following options are allowed: .RS 4 .IP "name" 4 .IX 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 \f(CW\*(C`name\*(C'\fR to \f(CW\*(C`foo\*(C'\fR: .Sp .Vb 1 \& $self\->form_state_config(\*(Aqname\*(Aq => \*(Aqfoo\*(Aq); .Ve .Sp then the hidden field in your template should look like this: .Sp .Vb 1 \& .Ve .Sp and the key in the user's session would look something like this: .Sp .Vb 1 \& form_state_foo_84eb13cfed01764d9c401219faa56d53 .Ve .IP "expires" 4 .IX Item "expires" Indicates when form state storage keys should expire and disappear from the user's session. Uses the same format as CGI::Session's \&\f(CW\*(C`expire\*(C'\fR. Defaults to 2 days (\f(CW\*(Aq2d\*(Aq\fR). To cancel expiration and make the form state last as long as the user's session does, use: .Sp .Vb 1 \& $self\->form_state_config(\*(Aqexpires\*(Aq => 0); .Ve .RE .RS 4 .RE .IP "param" 4 .IX Item "param" Read and set values in the form state storage. It acts like the \&\f(CW\*(C`param\*(C'\fR method typically does in modules such as \s-1CGI\s0, CGI::Application, CGI::Session, \f(CW\*(C`HTML::Template\*(C'\fR etc. .Sp .Vb 2 \& # set a value \& $self\->form_state\->param(\*(Aqsome_name\*(Aq => \*(Aqsome_value\*(Aq); \& \& # retrieve a value \& my $val = $self\->form_state\->param(\*(Aqsome_name\*(Aq); \& \& # set multiple values \& $self\->form_state\->param( \& \*(Aqsome_name\*(Aq => \*(Aqsome_value\*(Aq, \& \*(Aqsome_other_name\*(Aq => \*(Aqsome_other_value\*(Aq, \& ); \& \& # retrive the names of all the keys \& my @keys = $self\->form_state\->param; .Ve .IP "clear_params" 4 .IX Item "clear_params" Clear all of the values in the form state storage: .Sp .Vb 3 \& $self\->form_state\->param(\*(Aqname\*(Aq => \*(AqRoad Runner\*(Aq); \& $self\->form_state\->clear_params; \& print $self\->form_state\->param(\*(Aqname\*(Aq); # undef .Ve .IP "delete" 4 .IX Item "delete" Deletes the form_state storage from the user's session. .IP "id" 4 .IX Item "id" Returns the current value of the storage param \- the \*(L"hard to guess\*(R" portion of the session key. .Sp .Vb 1 \& my $id = $self\->form_state\->id; .Ve .IP "name" 4 .IX Item "name" Returns the current name being used for storage. Defaults to \f(CW\*(C`cap_form_state\*(C'\fR. .Sp .Vb 1 \& my $name = $self\->form_state\->name; .Ve .IP "session_key" 4 .IX Item "session_key" Returns the full key used for storage in the user's session. .Sp .Vb 1 \& my $key = $self\->form_state\->session_key; \& \& # Get the full form state hash \& my $data = $self\->session\->param($key); .Ve .Sp The following can be used to debug the form_state data: .Sp .Vb 2 \& use Data::Dumper; \& print STDERR Dumper $self\->session\->param($self\->form_state\->session_key); .Ve .SH "MOTIVATION" .IX Header "MOTIVATION" .SS "Why not just use hidden fields?" .IX Subsection "Why not just use hidden fields?" Hidden fields are not secure. The end user could save a local copy of your form, change the hidden fields and tamper with your app's form state. .SS "Why not just use the user's session?" .IX Subsection "Why not just use the user's session?" With \f(CW\*(C`CGI::Application::Plugin::FormState\*(C'\fR the data is associated with a particular instance of a form, not with the user. If the user gives up halfway through your multi-page form, you don't want their session to be cluttered up with the incomplete form state data. .PP If a user opens up your application in two browser windows (both sharing the same user session), each window should have it's own independent form state. .PP For instance, in an email application the user might have one window open for the inbox and another open for the outbox. If you store the value of \f(CW"current_mailbox"\fR in the user's session, then one of these windows will go to the wrong mailbox. .PP Finally, the user's session probably sticks around longer than the form state should. .SH "AUTHOR" .IX Header "AUTHOR" Michael Graham, \f(CW\*(C`\*(C'\fR .SH "BUGS" .IX Header "BUGS" Please report any bugs or feature requests to \&\f(CW\*(C`bug\-cgi\-application\-plugin\-formstate@rt.cpan.org\*(C'\fR, or through the web interface at . I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. .SH "ACKNOWLEDGEMENTS" .IX Header "ACKNOWLEDGEMENTS" Thanks to Richard Dice and Cees Hek for helping me sort out the issues with this approach. .PP The informative error message text used for when this module is loaded before your app actually \f(CW@ISA\fR \f(CW\*(C`CGI::Application\*(C'\fR object was stolen from Cees's CGI::Application::Plugin::TT module. .SH "COPYRIGHT & LICENSE" .IX Header "COPYRIGHT & LICENSE" Copyright 2005 Michael Graham, All Rights Reserved. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.