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.

However, if you use try to use the current run mode as your return run
mode and there's an e.g. validation problem, check_rm() ends up
recursively calling that run mode and things go south.

Here's a littlish app that demonstrates the problem.  An empty box, or
a number in the box are ok.  Anything else (e.g. a letter) cause it to
recurse.

################################################################
use CGI::Application::Server;

my $server = CGI::Application::Server->new();
$server->entry_points({'/mypage' => 'MyApp',
                      });
$server->run();

package MyApp;

use base 'CGI::Application';
use CGI::Application::Plugin::ValidateRM;
use Data::FormValidator::Constraints qw(:regexp_common); # FV_num_int...

sub setup {
  my $self = shift;
  $self->start_mode('mypage');
  $self->run_modes( [ 'mypage' ] );
}

sub mypage {
  my $self = shift;
  my $errs = shift;
  my %params;

  my $r = $self->check_rm('mypage',
                          {
                           required => ['widget'],
                           defaults => {widget => 1},
                           constraint_methods => {widget => FV_num_int()},
                           msgs => {any_errors => 'dfv_errors',
                                    prefix => 'err_',
                                   },
                          }
                         )
    || return $self->check_rm_error_page;

  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);
}
################################################################

One can get the behaviour that I expected and prefer by making the
following change to ValidateRM.  I don't expect that everyone would
want it to be the default, since the form no longer contains the bad
entry as a hint at what needs to be fixed [but there's still an error
message available], but I don't know where to hang the configuration
information.

################################################################
--- /usr/local/lib/perl5/site_perl/5.8.8/CGI/Application/Plugin/ValidateRM.pm   
2007-08-31 08:30:51.000000000 -0700
+++ lib/CGI/Application/Plugin/ValidateRM.pm    2007-08-31 08:36:07.000000000 
-0700
@@ -89,6 +89,10 @@
         my $before_rm = $self->{__CURRENT_RUNMODE};
         $self->{__CURRENT_RUNMODE} = $return_rm if 
($INC{'CGI/Application/Plugin/Forward.pm'});
 
+       # delete any invalid parameters so that they don't screw 
+       # up recursive calls.
+       map {$self->query->delete($_)} $r->invalid if ($r->has_invalid);
+
         my $return_page = $self->$return_rm($r->msgs);
 
         $self->{__CURRENT_RUNMODE} =  $before_rm;
################################################################

Have other folks stumbled on this?  How would *you* layout a page like
this?

Thanks,

g.

---------------------------------------------------------------------
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]

Reply via email to