Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Form Validation

by Wally Hartshorn (Hermit)
on Oct 06, 2003 at 21:57 UTC ( [id://297098]=note: print w/replies, xml ) Need Help??


in reply to OT - Apache Toolkit - PHP & mod_perl

You indicated that one of the main things he would be doing in PHP is form validation. If that's the case, you might want to take a look at Data::FormValidator. That makes form validation in Perl a snap (to the point where, frankly, it won't take up a significant amount of his time).

Here's an example from the program I'm just finishing up:

sub validate_fields { my $self = shift; my ($results, $error_page) = $self->check_rm( 'show_form', { required => [qw/ case_name file_type date interim_final case_summary /], optional => [qw/ alias penalty_amount compliance_plan_requirements compliance_plan_requirements_value /], require_some => { program_name => [ 1, qw/ pn_open_burning pn_asbestos pn_permit pn_monitoring /], }, dependencies => { reimburse_value => ['reimburse'], }, constraints => { interim_final => qr/^interim$|^final$/i, order_date => sub { return ParseDate(shift); }, }, filters => ['trim'], msgs => { any_errors => 'err__', prefix => 'err_', missing => 'Required Field', }, }, ); return $error_page; }

This is actually part of a CGI::Application-based program, so we're using CGI::Application::ValidateRM instead (which just inherits from Data::FormValidator and calls the appropriate CGI::Application run_mode if necessary).

We just list which fields are required, which are optional, which are a "choose at least one of these" group, which must have values that match a certain regex, are required only if some other field has a value, etc. We even verify that the date they've entered is valid.

If there's a problem, the form is automagically filled in with the values that they've entered, along with text indicating which fields have problems.

When combined with HTML::FillInForm, that leaves very little fiddling that we have to do to process web forms. That allows us to focus on the actually business logic of the program.

Anyway, PHP would work fine for doing form validation, but if that's the primary way in which it will be used, you might be better off in the long run just using Perl. It should take just a little time to get something like this going.

Wally Hartshorn

(Plug: Visit JavaJunkies, PerlMonks for Java)

Replies are listed 'Best First'.
Re: Form Validation
by jdtoronto (Prior) on Oct 07, 2003 at 01:31 UTC
    This is actually the combination I am looking at. We dynamically generate a lot of forms now, mainly from a four year old script that uses HERE documents! But - the clients want a variety of customisable forms, they all collect basically the same info and the validation varies only a little. So I have designed, but not yet coded, a dynamic form generator that requires the template be loaded into a BLOB in a MySQL database and the validation information will be retrieved from other fields - all of which gets merged with a standard form block which is customised using CSS. The client ID is implicit in the URL and the correct form is built - on the fly & correctly - despite changes in the surrounding info on the page and style/colour changes.

    jdtoronto

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://297098]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-16 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found