check is the recommended method to use to validate forms. It returns its results as a Data::FormValidator::Results object. A deprecated method validate described below is also available, returning its results as an array.

Here, check() is used as a class method, and takes two required parameters.

The first a reference to the data to be be validated. This can either be a hash reference, or a CGI.pm-like object. In particular, the object must have a param() method that works like the one in CGI.pm does. CGI::Simple and Apache::Request objects are known to work in particular. Note that if you use a hash reference, multiple values for a single key should be presented as an array reference.

This is a reference to an array which contains the name of the missing fields. Those are the fields that the user forget to fill or filled with spaces. These fields may comes from the required list or the dependencies list.

This is a reference to an array which contains the name of the fields which failed one or more of their constraint checks. If there are no invalid fields, an empty arrayref will be returned.

Fields defined with multiple constraints will have an array ref returned in the @invalids array instead of a string. The first element in this array is the name of the field, and the remaining fields are the names of the failed constraints.

Applying defaults to more than one input profile. There are some parts of the validation profile that you might like to re-use for many form validations.

To facilitate this, new() takes a second argument, a hash reference. Here the usual input profile definitions can be made. These will act as defaults for any subsequent calls to check() on this object.

Currently the logic for this is very simple. Any definition of a key in your validation profile will completely overwrite your default value.

This means you can't define two keys for constraint_regexp_map and expect they will always be there. This kind of feature may be added in the future.

The exception here is definitions for your msgs key. You will safely be able to define some defaults for the top level keys within msgs and not have them clobbered just because msgs was defined in a validation profile.

One way to use this feature is to create your own sub-class that always provides your defaults to new().

Another option is to create your own wrapper routine which provides these defaults to new(). Here's an example of a routine you might put in a CGI::Application super-class to make use of this feature:

require_some => {
# require any two fields from this group
city_or_state_or_zipcode => [ 2, qw/city state zipcode/ ],
}

This is a reference to a hash which defines groups of fields where 1 or more fields from the group should be required, but exactly which fields doesn't matter. The keys in the hash are the group names. These are returned as "missing" unless the required number of fields from the group has been filled in. The values in this hash are array references. The first element in this array should be the number of fields in the group that is required. If the first field in the array is not an a digit, a default of "1" will be used.

This is an array reference which contains the name of optional fields. These are fields which MAY be present and if they are, they will be checked for valid input. Any fields not in optional or required list will be reported as unknown.

This is a regular expression used to specify additional fields which are optional. For example, if you wanted all fields names that begin with user_ to be optional, you could use the regular expression, /^user_/

This is for the case where an optional field has other requirements. The dependent fields can be specified with an array reference.

If the dependencies are specified with a hash reference then the additional constraint is added that the optional field must equal a key for the dependencies to be added.

If the dependencies are specified as a code reference then the code will be executed to determine the dependent fields. It is passed two parameters, the object and the value of the field, and it should return an array reference containing the list of dependent fields.

Any fields in the dependencies list that are missing when the target is present will be reported as missing.

dependency_groups => {
# if either field is filled in, they all become required
password_group => [qw/password password_confirmation/],
}

This is a hash reference which contains information about groups of interdependent fields. The keys are arbitrary names that you create and the values are references to arrays of the field names in each group.

This is a regular expression used to specify additional fields which are dependent. For example, if you wanted to add dependencies for all fields which meet a certain criteria (such as multiple items in a shopping cart) where you do not know before hand how many of such fields you may have.

This is for the case where an optional field can trigger other optional fields. The dependent optional fields can be specified with an array reference.

If the dependent optional fields are specified with a hash reference, then an additional constraint is added that the optional field must equal a key for the additional optional fields to be added.

If the dependent optional fields are specified as a code reference then the code will be executed to determine the additional optional fields. It is passed two parameters, the object and the value of the field, and it should return an array reference containing the list of additional optional fields.

Sometimes a field will need to trigger additional dependencies but you only require some of the fields. You cannot set them all to be dependent as you might only have some of them, and you cannot set them all to be optional as you must have some of them. This method allows you to specify this in a similar way to the equire_some method but dependent upon other values. In the example above if the AddressID submitted is "new" then at least 1 of HouseName and HouseNumber must also be supplied. See require_some for the valid options for the return.

This is a hash reference that maps regular expressions to default values to use for matching optional or required fields.

It's useful if you have generated many checkbox fields with the similar names. Since checkbox fields submit nothing at all when they are not checked, it's useful to set defaults for them.

Note that it doesn't make sense to use a default for a field handled by optional_regexp or required_regexp. When the field is not submitted, there is no way to know that it should be optional or required, and thus there's no way to know that a default should be set for it.

A hash ref with field names as keys. Values are array references of built-in filters to apply (trim,digit,etc) or an anonymous subroutine which should take one parameter, the field value and return the (possibly) modified value.

field_filter_regexp_map => {
# Upper-case the first letter of all fields that end in "_name"
qr/_name$/ => ['ucfirst'],
},

'field_filter_regexp_map' is used to apply filters to fields that match a regular expression. This is a hash reference where the keys are the regular expressions to use and the values are references to arrays of filters which will be applied to specific input fields. Just as with 'field_filters', you can you use a built-in filter or use a coderef to supply your own.

A hash ref which contains the constraints that will be used to check whether or not the field contains valid data.

Note: To use the built-in constraints, they need to first be loaded into your name space using the syntax above. (Unless you are using the old constraints key, documented in "BACKWARDS COMPATIBILITY").

The keys in this hash are field names. The values can be any of the following:

If this field is named in untaint_constraint_fields or untaint_regexp_map, or untaint_all_constraints is effective, be aware of the following: If you write your own regular expressions and only match part of the string then you'll only get part of the string in the valid hash. It is a good idea to write you own constraints like /^regex$/. That way you match the whole string.

# Notice the use of 'pop'--
# the object is the first arg passed to the method
# while the value is the second, and last arg.
my_zipcode_field => sub { my $val = pop; return $val =~ '/^\d{5}$/' },
# OR you can reference a subroutine, which should work like the one above
my_zipcode_field => \&my_validation_routine,
# An example of setting the constraint name.
my_zipcode_field => sub {
my ($dfv, $val) = @_;
$dfv->set_current_constraint_name('my_constraint_name');
return $val =~ '/^\d{5}$/'
},

use Data::FormValidator::Constraints qw(:closures);
# In your profile.
constraint_method_regexp_map => {
# All fields that end in _postcode have the 'postcode' constraint applied.
qr/_postcode$/ => postcode(),
},

A hash ref where the keys are the regular expressions to use and the values are the constraints to apply.

If one or more constraints have already been defined for a given field using constraint_methods, constraint_method_regexp_map will add an additional constraint for that field for each regular expression that matches.

If this field is set, all form data that passes a constraint will be untainted. The untainted data will be returned in the valid hash. Untainting is based on the pattern match used by the constraint. Note that some constraint routines may not provide untainting.

Specifies that one or more fields will be untainted if they pass their constraint(s). This can be set to a single field name or an array reference of field names. The untainted data will be returned in the valid hash.

Specifies that certain fields will be untainted if they pass their constraints and match one of the regular expressions supplied. This can be set to a single regex, or an array reference of regexes. The untainted data will be returned in the valid hash.

The above example would untaint the fields named some_field_1, and some_field_2 but not some_field.

This can be set to a true value to cause optional fields with empty values to be included in the valid hash. By default they are not included-- this is the historical behavior.

This is an important flag if you are using the contents of an "update" form to update a record in a database. Without using the option, fields that have been set back to "blank" may fail to get updated.

# load all the constraints and filters from these modules
validator_packages => [qw(Data::FormValidator::Constraints::Upload)],

This key is used to define other packages which contain constraint routines or filters. Set this key to a single package name, or an arrayref of several. All of its constraint and filter routines beginning with 'match_', 'valid_' and 'filter_' will be imported into Data::FormValidator. This lets you reference them in a constraint with just their name, just like built-in routines. You can even override the provided validators.

The %s will be replaced with the message. The effect is that the message will appear in bold red with an asterisk before it. This style can be overridden by simply defining "dfv_errors" appropriately in a style sheet, or by providing a new format string.

Here's a more complex example that shows how to provide your own default message strings, as well as providing custom messages per field, and handling multiple constraints:

This is a new feature. While it expected to be forward-compatible, it hasn't yet received the testing the rest of the API has.

If the built-in message generation doesn't suit you, it is also possible to provide your own by specifying a code reference:

msgs => \&my_msgs_callback

This will be called as a Data::FormValidator::Results method. It may receive as arguments an additional hash reference of control parameters, corresponding to the key names usually used in the msgs area of the profile. You can ignore this information if you'd like.

If you have an alternative error message handler you'd like to share, stick in the Data::FormValidator::ErrMsgs name space and upload it to CPAN.

A number of parts of the input profile specification include array references as their values. In any of these places, you can simply use a string if you only need to specify one value. For example, instead of

You can pass more than one value into a constraint routine. For that, the value of the constraint should be a hash reference. If you are creating your own routines, be sure to read the section labeled "WRITING YOUR OWN CONSTRAINT ROUTINES", in the Data::FormValidator::Constraints documentation. It describes a newer and more flexible syntax.

Using the original syntax, one key should be named constraint and should have a value set to the reference of the subroutine or the name of a built-in validator. Another required key is params. The value of the params key is a reference to an array of the other elements to use in the validation. If the element is a scalar, it is assumed to be a field name. The field is known to Data::FormValidator, the value will be filtered through any defined filters before it is passed in. If the value is a reference, the reference is passed directly to the routine. Don't forget to include the name of the field to check in that list, if you are using this syntax.

Multiple constraints can be applied to a single field by defining the value of the constraint to be an array reference. Each of the values in this array can be any of the constraint types defined above.

When using multiple constraints it is important to return the name of the constraint that failed so you can distinguish between them. To do that, either use a named constraint, or use the hash ref method of defining a constraint and include a name key with a value set to the name of your constraint. Here's an example:

For even more advanced validation, you will likely want to read the documentation for other modules in this distribution, linked below. Also keep in mind that the Data::FormValidator profile structure is just another data structure. There is no reason why it needs to be defined statically. The profile could also be built on the fly with custom Perl code.

This is a reference to an array which contains the name of the missing fields. Those are the fields that the user forget to fill or filled with spaces. These fields may comes from the required list or the dependencies list.

This is a reference to an array which contains the name of the fields which failed one or more of their constraint checks.

Fields defined with multiple constraints will have an array ref returned in the @invalids array instead of a string. The first element in this array is the name of the field, and the remaining fields are the names of the failed constraints.

Using a hash reference for a constraint permits the passing of multiple arguments. Required arguments are constraint or constraint_method. Optional arguments are name and params.

A name on a constraints 'glues' the constraint to its error message in the validator profile (refer msgs section below). If no name is given then it will default to the value of constraint or constraint_method IF they are NOT a CODE ref or a RegExp ref.

The params value is a reference to an array of the parameters to pass to the constraint method. If an element of the params list is a scalar, it is assumed to be naming a key of the %input_hash and that value is passed to the routine. If the parameter is a reference, then it is treated literally and passed unchanged to the routine.

If you are using the older constraint over the new constraint_method then don't forget to include the name of the field to check in the params list. constraint_method provides access to this value via the get_current_* methods (refer Data::FormValidator::Constraints)

This is a supported but deprecated profile key. Using constraint_methods_regexp_map is recommended instead.

constraint_regexp_map => {
# All fields that end in _postcode have the 'postcode' constraint applied.
qr/_postcode$/ => 'postcode',
},

A hash ref where the keys are the regular expressions to use and the values are the constraints to apply.

If one or more constraints have already been defined for a given field using "constraints", constraint_regexp_map will add an additional constraint for that field for each regular expression that matches.