note
rhesa
Lots of questions here!
<p>
First off, remember that HTTP is a stateless protocol, which means that the server(s) do not share any data between requests. You need to do that explicitly, either by putting data in the url, or in a form, or in a session.
<p>
<h3>Sharing state between requests</h3>
You have essentially three options:
<ol>
<li>Using url parameters: <c>myapp?rm=edit;user=1</c> produces a page, and adds <c>user=1</c> to the relevant urls. E.g. <c>myapp?rm=save;user=1</c>. The next request finds the user id with <c>$self->query->param('user')</c>.
<li>Using form fields: The same request produces a form with a hidden field: <c>
<form action="myapp">
<input type="hidden" name="rm" value="save">
<input type="hidden" name="user" value="1">
</c>
The next request finds the user id with <c>$self->query->param('user')</c> as well.
<li>using a session: The same request creates a session, stores <c>user=>1</c> in it, and sends a cookie with the session id in it along with the output. The next request looks in the session for the "user" parameter.
</ol>
<h3>How to use sessions with CGI::Application</h3>
Save yourself headaches, and install [mod://CGI::Application::Plugin::Session]. Configuration is pretty straightforward. Usage is dead simple too:
<c>
sub edit {
my $self = shift;
my $user_id = $self->query->param( 'user' );
$self->session->param( user => $user_id ); # store it
...
}
sub save {
my $self = shift;
my $user_id = $self->session->param( 'user' ); # retrieve it
...
}
</c>
<p>
<h3>Form validation strategy</h3>
It's handy to have form display and form processing in separate methods. The general pattern of form processing is
<ol>
<li>Gather input
<li>Validate input
<li><ul><li>If invalid, display the form again with error messages
<li>If valid, process the input
</li>
</ol>
Something like this:
<c>
sub display_form {
...
}
sub process_form {
my $self = shift;
my @input = data_from_query_or_session();
# use CAP::ValidateRM here
my @errors = validate( $profile, @input );
if( @errors ) {
# HTML::FillInForm makes this easier
return $self->display_form( @errors );
}
# submission was ok, process the data
...
}
</c>
Hope that helps :)
722232
722232