Data::FormValidator::Profile - Profile object for Data::FormValidator
use Data::FormValidator; use Data::FormValidator::Profile; # create a new DFV::Profile object $profile = Data::FormValidator::Profile->new( { optional => [qw( this that )], required => [qw( some other thing )], } ); # query the optional/required fields in the profile @optional = $profile->optional(); @required = $profile->required(); # reduce the profile to just a limited set of fields $profile->only( qw(this that) ); # remove fields from the profile $profile->remove( qw(some other thing) ); # add a new field to the profile $profile->add( 'username', required => 1, filters => 'trim', constraints => [ ... ], msgs => { constraints => { file_max_bytes => 'too big', }, }, ); # call chaining, to make manipulation quicker $profile->only(qw( this that other )) ->remove(qw( that )) ->add(qw( foo )) ->check($data); # use the profile to validate data $data = { ... }; $res = $profile->check($data); # ... or $res = Data::FormValidator->check( $data, $profile->profile() );
Data::FormValidator::Profile provides an interface to help manage Data::FormValidator profiles.
Data::FormValidator::Profile
Data::FormValidator
I found that I was frequently using Data::FormValidator profiles to help define my DB constraints and validation rules, but that depending on the context I was working in I may only be manipulating a small handful of the fields at any given point. Although I could query my DB layer to get the default validation profile, I was really only concerned with the rules for two or three fields. Thus, Data::FormValidator::Profile, to help make it easier to trim profiles to include only certain sets of fields in the profile.
All said, though, Data::FormValidator::Profile has some limitations that you need to be aware of.
It only removes fields from the following profile attributes:
required optional defaults field_filters constraints constraint_methods
NO effort is made to update dependencies, groups, require_some, or anything based on a regexp match. Yes, that does mean that this module is limited in its usefulness if you've got really fancy Data::FormValidator profiles. That said, though, I'm not using anything that fancy, so it works for me.
To use the profile with Data::FormValidator, use either the form of:
$profile->check($data)
or
Data::FormValidator->check($data, $profile->profile)
Data::FormValidator won't accept a blessed object when calling Data::FormValidator->check(), so you need to call $profile->profile() to turn the profile into a HASHREF first.
Data::FormValidator->check()
$profile->profile()
Unless you're doing anything fancier and you've got an actual Data::FormValidator object that you're working with, its easier/simpler to just call $profile->check($data); that's the recommended interface.
Creates a new DFV::Profile object, based on the given profile (which can be provided either as a HASH or a HASHREF).
Checks the given $data against the profile. This method simply acts as a short-hand to Data::FormValidator->check($data,$profile->profile).
$data
Data::FormValidator->check($data,$profile->profile)
Returns the actual profile, as a hash-ref. You need to call this method when you want to send the profile through to Data::FormValidator to do data validation.
Returns the list of "required" fields in the validation profile.
Returns the list of "optional" fields in the validation profile.
Reduces the profile so that it only contains information on the given list of @fields.
@fields
Returns $self, to support call-chaining.
$self
Removes any of the given @fields from the profile.
Ensures that the given set of @fields are set as being optional (even if they were previously described as being required fields).
Ensures that the given set of @fields are set as being required (even if they were previously described as being optional fields).
Explicitly sets one or more %options into the profile. Useful when you KNOW exactly what you want to add/do to the profile.
%options
Adds the given $field to the validation profile, and sets up additional validation rules as per the provided %args.
$field
%args
If the field already exists in the profile, this method throws a fatal exception.
Acceptable %args include:
If non-zero, specifies that the field is required and is not an optional field (default is to be optional)
Default value for the field.
"dependencies" for this field. Replaces existing value.
"field_filters" to be applied. Replaces existing value.
"constraint_methods" for this field. Replaces existing value.
Hash-ref of "constraint messages" that are related to this field. Replaces existing values.
Here's an example to help show how the %args are mapped into a validation profile:
$profile->add( 'username', required => 1, filters => ['trim', 'lc'], constraints => FV_length_between(4,32), msgs => { length_between => 'Username must be 4-32 chars in length.', }, );
becomes:
{ required => [qw( username )], field_filters => { username => ['trim', 'lc'], }, constraint_methods => { username => FV_length_between(4,32), }, msgs => { constraints => { length_between => 'Username must be ...', }, }, }
Graham TerMarsch (cpan@howlingfrog.com)
Copyright (C) 2008, Graham TerMarsch. All Rights Reserved.
This is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Data::FormValidator.
To install Data::FormValidator::Profile, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::FormValidator::Profile
CPAN shell
perl -MCPAN -e shell install Data::FormValidator::Profile
For more information on module installation, please visit the detailed CPAN module installation guide.