| Class-DBI-DFV documentation | view source | Contained in the Class-DBI-DFV distribution. |
Class::DBI::DFV - check that your data is valid using DFV
package My::DBI;
use base 'Class::DBI::DFV';
__PACKAGE__->connection(...);
__PACKAGE__->table(...);
__PACKAGE__->columns( All => qw( id val_unique val_optional ) );
sub dfv_profile {
my $class = shift;
return {
filters => 'trim',
required => [qw/val_unique/],
constraint_methods => { val_unique => qr/^\d+$/ },
};
}
NOTE: this module is still under development - please see the bottom of the pod for how you can help.
Class::DBI::DFV combines the database abstraction of Class::DBI
with the data validation of Data::FormValidator. It allows you to
specify a DFV profile that the data must match. This profile is
applied when you do an insert or a set. If the profile does not
match then the normal Class::DBI->_croak method is called.
Class::DBI::DFV overides the validate_column_values method to do
the actual validating. Once it has validated the data it then calls
the parent class' validate_column_values method. There is no need
to call this in your code - it is called by Class::DBI. Be warned
though if you decide to override it as well.
eval { My::DBI->create( \%data ) }
|| warn "ERROR: ", Dumper( My::DBI->dfv_results->msgs );
The dfv_results method gives you access to the last results
produced by Data::FormValidator.
sub dfv_base_profile {
return {
filters => 'trim',
msgs => {
format => 'validation error: %s',
constraints => { unique_constraint => 'duplicate' },
},
};
}
You will find that there are many things that you will want to put in
all your profiles. If in your parent class you create
dfv_base_profile then the values in this will be combined with the
dfv_profile that you create. As a general rule anything that is
specified in the profile will override the values in the base profile.
This is a private method but as it changes your profile it is
documented here. The first thing it does is to combine the
dfv_base_profile and the dfv_profile.
Having done that it then looks at what columns you have in the
database and puts all the ones that are not in the profile's
required list in the optional list.
Finally it caches the profile to make execution faster. Make sure that you use sub refs if you want something to be executed each time the profile is parsed, eg:
defaults => {
wrong => rand(1000),
right => sub { rand(1000) },
},
The 'wrong' one will always return the same value - as the value is created when the profile is created. The 'right' one will be executed each time that the profile is applied and so will be different each time.
EXPERIMENTAL - this is a constraint that lets you check that the
database does not contain duplicate values. Please see the module
Local::Test in the test suite for usage. The way that this
constraint is used may well change.
Class::DBI - Simple Database Abstraction
Data::FormValidator - Validates user input (usually from an HTML form) based on input profile.
Edmund von der Burg - evdb@ecclestoad.co.uk
If you want to change something is Class::DBI::DFV I would be delighted to help. You can get the latest from http://svn.ecclestoad.co.uk/svn/class-dbi-dfv/trunk/. Anonymous access is read-only but if you have an idea please contact me and I'll create an account for you so you can commit too.
| Class-DBI-DFV documentation | view source | Contained in the Class-DBI-DFV distribution. |