On Friday 31 August 2007 9:29 am, George Hartzell wrote:
> I'd like to have a page that has some form fields at the top and
> displays content based on the current values of that form elsewhere on
> the page.
>
> I've tried to implement this by having a run mode that displays the
> page and is also the target of the form's submission. When things go
> well, the page should be displayed with content derived from the form
> fields, or with suitable default content if there are no params.
[.....snip.....]
> Have other folks stumbled on this? How would *you* layout a page like
> this?
Haven't run into this myself, but I generally split up the "display"
and "validate" parts of my run-modes into separate methods. Similar to what
Mark had outlined in his post entitled "RFC: plugin to handle pairs of
routines form display and processing" back on August 9th.
By splitting the "display" and "process" stuff out into separate methods you
should be able to eliminate the recursiveness issue that you're encountering.
You can also use "$self->param()" to help store and pass info back and forth
between the methods, so that when you're processing the form you can store
some data/text that you want to have re-shown on the form when you display it
again.
Note that I haven't -tried- the following code, but it'd look something like:
package MyApp;
use base 'CGI::Application';
use CGI::Application::Plugin::ValidateRM;
use Data::FormValidator::Constraints qw(:regexp_common);
sub setup {
my $self = shift;
$self->start_mode( 'mypage' );
$self->run_modes( [qw( mypage )] );
}
sub mypage {
my $self = shift;
if ($self->query->param('submit_button')) {
return $self->processMypage(@_);
}
return $self->showMypage(@_);
}
sub showMypage {
my ($self, $errs) = @_;
my $html = <<EOHTML;
<html>
<body>
<form name="silly" method="post" action="mypage">
<input type="text" name="widget" value="" size=25>
<input name="submit_button" type="submit" value="submit" />
</form>
</body>
</html>
EOHTML
return ($html);
}
sub processMypage {
my $self = shift;
# validate, showing error if invalid
my $res = $self->check_rm( 'mypage',
{ 'required' => [qw( widget )],
'defaults' => {
'widget' => 1,
},
'constraint_methods' => {
'widget' => FV_num_int(),
},
'msgs' => {
'any_errors' => 'dfv_errors',
'prefix' => 'err_',
},
} );
unless ($res) {
return $self->check_rm_error_page();
}
# data ok, show form again.
return $self->showMypage();
}
--
Graham TerMarsch
---------------------------------------------------------------------
Web Archive: http://www.mail-archive.com/[email protected]/
http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]