man > Data::FormValidator

πŸ“˜ NAME

Data::FormValidator - Validates user input (usually from an HTML form) based on input profile.

πŸš€ Quick Reference

Use CaseCommandDescription
Basic validationData::FormValidator->check(\%input, \%profile)🟒 Returns a Results object with has_invalid, has_missing, valid, invalid, missing
Define required fieldsrequired => [qw/field1 field2/]πŸ”΄ Missing fields reported
Define optional fieldsoptional => [qw/field1 field2/]🟑 Only checked if submitted
Apply constraintsconstraint_methods => { email => email() }βœ… Use built-in or custom constraints
Dependenciesdependencies => { "field" => [qw/dep1 dep2/] }πŸ”— If field present, dependents become required
Set defaultsdefaults => { country => "USA" }πŸ“₯ Missing fields get default values
Apply filtersfilters => ['trim']🧹 Pre-process all fields
Custom error messagesmsgs => { invalid => 'Invalid!', constraints => { 'name' => 'msg' } }πŸ—£οΈ Override default messages
Multiple constraints per fieldfield => [ \&constraint1, qr/regex/ ]πŸ”’ Apply several checks
Untaint validated datauntaint_all_constraints => 1πŸ”’ Automatically untaint passed values

πŸ“– SYNOPSIS

use Data::FormValidator;

my $results = Data::FormValidator->check(\%input_hash, \%dfv_profile);

if ($results->has_invalid or $results->has_missing) {
    # do something with $results->invalid, $results->missing
    # or  $results->msgs
}
else {
    # do something with $results->valid
}

πŸ“ DESCRIPTION

Data::FormValidator's main aim is to make input validation expressible in a simple format. It lets you define profiles which declare the required and optional fields and any constraints they might have. The results are provided as an object, making it easy to handle missing and invalid results, return error messages, or process the valid data.

πŸ” VALIDATING INPUT

βœ… check()

my $results = Data::FormValidator->check(\%input_hash, \%dfv_profile);

"check" is the recommended method. It returns a Data::FormValidator::Results object. The first argument is a hash reference or a CGI-like object with a param() method. The second is a profile reference.

⚠️ validate() (deprecated)

my( $valids, $missings, $invalids, $unknowns ) =
    Data::FormValidator->validate( \%input_hash, \%dfv_profile);

Returns a four-element array:

πŸ”§ new()

Advanced usage – loading multiple profiles, or applying defaults. Example:

my $dfv = Data::FormValidator->new({
    profile_1 => { # usual profile definition here },
    profile_2 => { # another profile definition },
});

Then call $dfv->check(\%input, 'profile_1').

πŸ“‹ INPUT PROFILE SPECIFICATION

An input profile is a hash reference containing one or more of the following keys:

πŸ”΄ required

Array reference of field names that must be present. Missing fields are reported.

πŸ”΄ required_regexp

required_regexp => qr/city|state|zipcode/,

Regular expression to specify additional required fields.

πŸ”΄ require_some

require_some => {
    city_or_state_or_zipcode => [ 2, qw/city state zipcode/ ],
}

Hash of groups where at least n fields from the group must be filled.

🟑 optional

optional => [qw/meat coffee chocolate/],

These fields MAY be present; if so, they are validated.

🟑 optional_regexp

optional_regexp => qr/_province$/,

Regex to mark additional optional fields.

πŸ”— dependencies

dependencies => {
    "cc_no" => [ qw( cc_type cc_exp ) ],
    "pay_type" => {
        check => [ qw( check_no ) ],
    },
    "cc_type" => sub {
        my $dfv  = shift;
        my $type = shift;
        return [ 'cc_cvv' ] if ($type eq "VISA" || $type eq "MASTERCARD");
        return [ ];
    },
},

Makes fields required when a specific optional field is present. Can be array, hash (value‑dependent), or code ref.

πŸ”— dependency_groups

dependency_groups => {
    password_group => [qw/password password_confirmation/],
}

If any field in the group is filled, all become required.

πŸ”— dependencies_regexp

dependencies_regexp => {
    qr/Line\d+\_ItemType$/ => sub {
        my $dfv = shift;
        my $itemtype = shift;
        my $field = shift;
        if ($type eq 'NeedsBatteries') {
            my ($prefix, $suffix) = split(/\_/, $field);
            return([$prefix . '_add_batteries']);
        } else {
            return([]);
        }
    },
},

Regex‑based dependencies for dynamic field names.

🟑 dependent_optionals

dependent_optionals => {
    "delivery_address" => [ qw( delivery_notes ) ],
    "delivery_type" => {
        collection => [ qw( collection_notes ) ],
    },
    "callback_type" => sub {
        my $dfv = shift;
        my $type = shift;
        if ($type eq 'phone' || $type eq 'email') {
            return(['additional_notes']);
        } else {
            return([]);
        }
    },
},

Makes optional fields conditionally required when another optional field is present.

πŸ”— dependent_require_some

dependent_require_some => {
    AddressID => sub {
        my $dfv = shift;
        my $value = shift;
        if ($value eq 'new') {
            return({
                house_name_or_number => [ 1, 'HouseName', 'HouseNumber' ],
            });
        } else {
            return;
        }
    },
}

Conditionally applies require_some logic based on another field's value.

πŸ“₯ defaults

defaults => {
    country => "USA",
},

Default values for missing fields. Values can be code refs.

πŸ“₯ defaults_regexp_map

defaults_regexp_map => {
    qr/^opt_/ => 1,
},

Set defaults for fields matching a regex (e.g., unchecked checkboxes).

🧹 filters

filters => ['trim'],

Array of filters applied to ALL optional and required fields before constraints.

🧹 field_filters

field_filters => {
    cc_no => ['digit'],
},

Per‑field filters.

🧹 field_filter_regexp_map

field_filter_regexp_map => {
    qr/_name$/ => ['ucfirst'],
},

Apply filters to fields matching a regex.

βœ… constraint_methods

use Data::FormValidator::Constraints qw(:closures);

constraint_methods => {
    cc_no   => cc_number({fields => ['cc_type']}),
    cc_type => cc_type(),
    cc_exp  => cc_exp(),
},

Hash ref of field names to constraints. Values can be:

βœ… constraint_method_regexp_map

constraint_method_regexp_map => {
    qr/_postcode$/ => postcode(),
},

Apply constraints to fields matching a regex.

πŸ”’ untaint_all_constraints

untaint_all_constraints => 1,

Untaint all data that passes constraints.

πŸ”’ untaint_constraint_fields

untaint_constraint_fields => [qw(zipcode state)],

Untaint specific fields.

πŸ”’ untaint_regexp_map

untaint_regexp_map => [qr/some_field_\d/],

Untaint fields matching a regex.

🟒 missing_optional_valid

missing_optional_valid => 1

Include optional fields with empty values in the valid hash.

πŸ“¦ validator_packages

validator_packages => [qw(Data::FormValidator::Constraints::Upload)],

Load additional constraint and filter packages.

πŸ—£οΈ msgs

msgs => {
    prefix => 'error_',
    missing => 'Not Here!',
    invalid => 'Problematic!',
    invalid_separator => ' <br /> ',
    format => 'ERROR: %s',
    constraints => {
        'date_and_time' => 'Not a valid time format',
    },
    any_errors => 'some_errors',
}

Custom error message formatting. Also supports a callback: msgs => \&my_msgs_callback.

πŸ› debug

debug => 1,

Prints debug info to STDERR.

πŸ“Œ A shortcut for array refs

Where an array ref is expected, you can use a single string: filters => 'trim' instead of filters => ['trim'].

πŸ“Œ A note on regular expression formats

Both qr// and deprecated string style ('m/.../') are supported.

πŸ”— VALIDATING INPUT BASED ON MULTIPLE FIELDS

To pass multiple values to a constraint, use a hash reference:

cc_no => {
    constraint  => "cc_number",
    params      => [ qw( cc_no cc_type ) ],
},

See Data::FormValidator::Constraints for the newer syntax.

πŸ”’ MULTIPLE CONSTRAINTS

Use an array reference to apply multiple constraints to a single field:

my_zipcode_field => [
    'zip',
    {
        constraint_method => '/^406/',
        name              => 'starts_with_406',
    }
],

Each can be a named constraint, regex, or subroutine. Use name to distinguish failures.

🧠 ADVANCED VALIDATION

See other modules in the distribution. Profiles can be built dynamically with Perl code.

πŸ”„ BACKWARDS COMPATIBILITY

⚠️ validate()

Deprecated alternative to check(). Returns four array elements as described previously.

πŸ“Œ constraints (profile key)

Deprecated – use constraint_methods instead.

constraints => {
    cc_no => {
        constraint  => "cc_number",
        params      => [ qw( cc_no cc_type ) ],
    },
    cc_type => "cc_type",
    cc_exp  => "cc_exp",
},

πŸ“Œ Hashref style of specifying constraints

Older technique for naming constraints or supplying multiple parameters:

cc_no => {
    constraint  => "cc_number",
    params      => [ qw( cc_no cc_type ) ],
},

last_name => {
    name => "ends_in_name",
    constraint => qr/_name$/,
},

πŸ“Œ constraint_regexp_map (profile key)

Deprecated – use constraint_methods_regexp_map.

constraint_regexp_map => {
    qr/_postcode$/ => 'postcode',
},

πŸ“š SEE ALSO

πŸ“¦ Other modules in this distribution:

🌐 A sample application by the maintainer:

Validating Web Forms with Perl, http://mark.stosberg.com/Tech/perl/form-validation/

πŸ”— Related modules:

🌍 Document Translations:

Japanese: http://perldoc.jp/docs/modules/

πŸ“¦ Distributions which include Data::FormValidator:

FreeBSD: p5-Data-FormValidator, Debian: libdata-formvalidator-perl

πŸ‘ CREDITS

Some input validation functions from MiniVend by Michael J. Heins. Credit card checksum validation from Bruce Albrecht.

πŸ› BUGS

Bug reports and patches welcome. Tests with Test::More are helpful. http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-FormValidator

🀝 CONTRIBUTING

Maintained on Github: https://github.com/dnmfarrell/Data-FormValidator

πŸ‘€ AUTHOR

Currently maintained by David Farrell <dfarrell AT cpan.org>. Parts Copyright 2001‑2006 Mark Stosberg. Copyright 1999 Francis J. Lacoste and iNsu Innovations Inc. Parts Copyright 1996‑1999 Michael J. Heins and Bruce Albrecht.

πŸ“„ LICENSE

This program is free software; you can redistribute it and/or modify it under the terms as perl itself.

Data::FormValidator
πŸ“˜ NAME πŸš€ Quick Reference πŸ“– SYNOPSIS πŸ“ DESCRIPTION πŸ” VALIDATING INPUT
βœ… check() ⚠️ validate() (deprecated) πŸ”§ new()
πŸ“‹ INPUT PROFILE SPECIFICATION
πŸ”΄ required πŸ”΄ required_regexp πŸ”΄ require_some 🟑 optional 🟑 optional_regexp πŸ”— dependencies πŸ”— dependency_groups πŸ”— dependencies_regexp 🟑 dependent_optionals πŸ”— dependent_require_some πŸ“₯ defaults πŸ“₯ defaults_regexp_map 🧹 filters 🧹 field_filters 🧹 field_filter_regexp_map βœ… constraint_methods βœ… constraint_method_regexp_map πŸ”’ untaint_all_constraints πŸ”’ untaint_constraint_fields πŸ”’ untaint_regexp_map 🟒 missing_optional_valid πŸ“¦ validator_packages πŸ—£οΈ msgs πŸ› debug πŸ“Œ A shortcut for array refs πŸ“Œ A note on regular expression formats
πŸ”— VALIDATING INPUT BASED ON MULTIPLE FIELDS πŸ”’ MULTIPLE CONSTRAINTS 🧠 ADVANCED VALIDATION πŸ”„ BACKWARDS COMPATIBILITY
⚠️ validate() πŸ“Œ constraints (profile key) πŸ“Œ Hashref style of specifying constraints πŸ“Œ constraint_regexp_map (profile key)
πŸ“š SEE ALSO
πŸ“¦ Other modules in this distribution: 🌐 A sample application by the maintainer: πŸ”— Related modules: 🌍 Document Translations: πŸ“¦ Distributions which include Data::FormValidator:
πŸ‘ CREDITS πŸ› BUGS 🀝 CONTRIBUTING πŸ‘€ AUTHOR πŸ“„ LICENSE

Generated by phpman v4.10.0-16-g1a0e228 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-22 21:52 @216.73.217.0
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!

^_top_^