Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I spend a lot of time writing perl to process HTML forms. I soon realized that I was writing very similar code in each case. Some common tasks of form validation include:
  • making sure required fields are populated
  • trimming leading and trailing whitespace off of input.
  • handling fields that become required when another field is filled in
  • validating common input types -- answering such questions as "is this a valid email?", "is this valid zipcode?", "is this valid telephone number?".
Data::FormValidator helps with all these tasks and more. Outside of the functions it provides, I find declaring the the form validation profile through it's interface to be useful. Here's the example from the documentation:
    {
        customer_infos => {
            optional     =>
                [ qw( company fax country ) ],
            required     =>
                [ qw( fullname phone email address city state zipcode ) ],
            constraints  =>
                {
                    email       => "email",
                    fax         => "american_phone",
                    phone       => "american_phone",
                    zipcode     => '/^\s*\d{5}(?:[-]\d{4})?\s*$/',
                    state       => "state",
                },
            defaults => {
                country => "USA",
            },
        },
        customer_billing_infos => {
             optional       => [ "cc_no" ],
             dependencies   => {
                "cc_no" => [ qw( cc_type cc_exp ) ],
             },
             constraints => {
                cc_no      => {  constraint  => "cc_number",
                                 params      => [ qw( cc_no cc_type ) ],
                                },
                cc_type => "cc_type",
                cc_exp  => "cc_exp",
              }
            filters       => [ "trim" ],
            field_filters => { cc_no => "digit" },
        },
    }
Any validation that you want to yourself you can add in, so you aren't limited to just the options that this module provides. Additionally, HTML::FormValidator doesn't force you to handle the form validations errors in any particular way. Instead it returns the results like this:
    my( $valids, $missings, $invalids, $unknowns ) =
        $validator->validate( \%fdat, "customer_infos" );
Here $valids will be a hash ref, and the other values will be array refs. A nice side effect of this arrangement is that if you've named your form fields with the same names as some database columns, you can now pass your $valids hash ref directly to a module like DBIx::Abstract to insert the results into a database, auto-quoting the values along the way.

Room for improvement

While I'm a fan of the module and find it very usable, it doesn't feel quite done yet. You can read and about my ideas to improve it.

An example

I've also put together an example of using Data::FormValidator for you to review. It demonstrates how you can use Data::FormValidator along with some other modules to easily display form validation results on the same page as the form, with the former values already filled in.

In reply to Data::FormValidator by markjugg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found