On Fri, 07 Jul 2006 09:25:10 -0400, Lance A. Brown wrote:

Hi Wojciech, Lance

Here's how I do that.

Complications:
(a) 2 separate CGI forms can have the same drop-down campus menu, one for the
academic's office campus and one for their teaching campus. Hence the parameter
$id_name in sub validate_campus_name. $id_name holds the name of the CGI form
field which is the menu
(b) Besides the menu, there is a text field for entering a campus name not in
the menu. Hence the 'optional' stuff

(1) In my CGI::App:
# -----------------------------------------------
# This run mode is called 2nd, or list_requests is.

sub unit
{
        my($self) = @_;

        $self -> log() -> info('Entered sub unit') if ($self -> 
param('verbose') );

        # Get the requestor's data from the 1st (requestor) CGI form.
        # Note: Run modes list_requests and unit use the same profile.

        my($result)             = Data::FormValidator -> check($self -> query(),
MorseCode::Validate -> build_profile_for_rm_unit($self) );
        my($form_data)  = scalar $result -> valid();

        CGI::Application::Util::Log2DB -> log_validation_results($self, 
$result);

        # If there is any error in the CGI data, redisplay the 1st (requestor) 
CGI
form.

        return $self -> build_requestor_form($form_data, $result -> msgs() ) if
($result -> has_invalid() || $result -> has_missing() );

        # Since there are no errors in the CGI data, we use the data to update 
the
user's attributes.
        etc
}
# -----------------------------------------------

(2) In Validate.pm:
# -----------------------------------------------

sub build_profile_for_rm_unit
{
        my($self, $cgi_app) = @_;

        return
        {
                constraint_methods      =>
                {
                        input_optional_campus_name => $self -> 
validate_campus_name($cgi_app,
'input_requestor_campus_id'),
                },
                filters                         => 'user_data', # Aka sub 
filter_user_data.
                msgs                            =>
                {
                        prefix                  => 'error_',
                },
                optional                        => 'input_optional_campus_name',
                required_regexp         => qr/^input_requestor_/,
                validator_packages      => ['MorseCode::Validate'],
        };

}       # End of build_profile_for_rm_unit.

# -----------------------------------------------

(3) Also in Validate.pm:
# -----------------------------------------------

sub validate_campus_name
{
        my($self, $cgi_app, $id_name) = @_;

        # Warning: This sub does not take $self as the first parameter.
        # See sub setup for details.

        return sub
        {
                my($dfv, $campus_name) = @_;

                # Test # 1: Is the campus name present? If empty, it's valid.
                # An empty name will be ignored, by using $id_name instead.
                # This works because we always offer the user a campus name 
menu and a text
field.
                # Notice that we don't actually bother to validate the
input_requestor_campus_id!

                return 1 if (! defined($campus_name) || ! length($campus_name) 
);

                # Test # 2: Is the campus name on file? If so, it's valid.
                # A known name will be ignored, by using $id_name
instead.
                # Also, we pass back the campus_id via the query object.
                # The campus_id in the session will be updated in Util.pm's sub
save_user_attributes()
                # after the flow of control passes back to Application.pm's sub 
unit.

                my(@campus) = MorseCode::Campus -> search(name_key => lc 
$campus_name);

                if (@campus)
                {
                        # We allow for the possibility that the user has typed 
in a name which is in
the menu.

                        $cgi_app -> query() -> param($id_name => $campus[0] -> 
id() );
                }
                else
                {
                        # If unknown, we add it to the database.

                        $cgi_app -> query() -> param($id_name => 
MorseCode::Campus ->
add_campus({name => $campus_name, name_key => lc $campus_name}));
                }

                return 1;
        }

}       # End of validate_campus_name.

# -----------------------------------------------

--
Cheers
Ron Savage, [EMAIL PROTECTED] on 8/07/2006
http://savage.net.au/index.html
Let the record show: Microsoft is not an Australian company



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