in reply to A CGI::Prototype respond() subroutine for Data::FormValidator users

We do something similar. Our validate hook is in 'respond_per_app' like so:

sub respond_per_app{ my $self = shift; # If we validate, go ahead with page-specific items. return 0 if $self->param($self->config_state_param) and $self->valid +ate(); # If we are here, something failed, so re-render ourselves. return $self; }

Our assumption is that if something failed in validation, we want to re-render the page we came from with an error message that tells the user what they did wrong. This should also give them back the bad params so they can see what they did wrong.

We then build the profile for Data::FormValidator on the fly based on the page we're on. The first part is set for the whole app like this:

sub validator_profile{ # Set some app-wide values for the profile for Data::FormValidator. { validator_packages => [qw( My::Constraints )], msgs => { format => '%s', invalid_seperator => '!', constraints => \%My::constraint_msgs }, }; }

Then in our config file, we have a hash with the constraint messages, and another that defines the required, optional, etc. fields. When the page loads, we have a 'load_attributes' method that builds out the profile. Then our validate method finalizes the profile and runs it through DFV. If anything fails, we load a data structure with errors to pass back to the template for display. These are displayed at the top of the page when we call ourselves again.

Building the profile and data structure for DFV is a little more work, but we've found many cases where dependencies are complex or there are extra params, so we can't just pass all params to DFV.

Replies are listed 'Best First'.
Re^2: A CGI::Prototype respond() subroutine for Data::FormValidator users
by metaperl (Curate) on Feb 07, 2005 at 18:39 UTC
    respond_per_app is not a standard call from CGI::Prototype's activate() method.

    What calls respond_per_app()?

      Whoops, sorry. That method is in CGI::Prototype::Hidden. The Hidden module inherits from CGI::Prototype and adds functionality because it has basic state information (i.e., where am I?). It gets that from a simple hidden field called _state which is the text version of the page name. You can call the hidden field whatever you like; you just need to write it to each page using wrapper.

      The idea is that there could eventually be different modules for different state mechanisms. So there is a simple one for Hidden fields. In the future there may be one for Cookies or MangledURLs, etc.