1 package DBIx::Class::Validation;
6 use base qw( DBIx::Class );
8 use English qw( -no_match_vars );
10 #local $^W = 0; # Silence C:D:I redefined sub errors.
11 # Switched to C::D::Accessor which doesn't do this. Hate hate hate hate.
13 our $VERSION = '0.01';
15 __PACKAGE__->mk_classdata( 'validation_module' => 'FormValidator::Simple' );
16 __PACKAGE__->mk_classdata( 'validation_profile' );
17 __PACKAGE__->mk_classdata( 'validation_auto' => 1 );
19 sub validation_module {
24 croak("Unable to load the validation module '$module' because $EVAL_ERROR") if ($EVAL_ERROR);
25 croak("The '$module' module does not support the check method") if (!$module->can('check'));
27 $class->_validation_module_accessor( $module );
34 $class->validation_module( $args{module} ) if (exists $args{module});
35 $class->validation_profile( $args{profile} ) if (exists $args{profile});
36 $class->validation_auto( $args{auto} ) if (exists $args{auto});
41 my %data = $self->get_columns();
42 my $module = $self->validation_module();
43 my $profile = $self->validation_profile();
44 my $result = $module->check( \%data => $profile );
45 return $result if ($result->success());
51 $self->validate if ($self->validation_auto());
52 $self->next::method(@_);
57 $self->validate if ($self->validation_auto());
58 $self->next::method(@_);
66 DBIx::Class::Validation - Validate all data before submitting to your database.
70 In your base DBIC package:
72 __PACKAGE__->load_components(qw/... Validation/);
74 And in your subclasses:
76 __PACKAGE__->validation(
77 module => 'FormValidator::Simple',
82 And then somewhere else:
84 eval{ $obj->validate() };
85 if( my $results = $EVAL_ERROR ){
93 __PACKAGE__->validation(
94 module => 'FormValidator::Simple',
99 Calls validation_module(), validation_profile(), and validation_auto() if the corresponding
102 =head2 validation_module
104 __PACKAGE__->validation_module('Data::FormValidator');
106 Sets the validation module to use. Any module that supports a check() method just like
107 Data::FormValidator's can be used here, such as FormValidator::Simple.
109 Defaults to FormValidator::Simple.
111 =head2 validation_profile
113 __PACKAGE__->validation_profile(
117 Sets the profile that will be passed to the validation module.
119 =head2 validation_auto
121 __PACKAGE__->validation_auto( 1 );
123 This flag, when enabled, causes any updates or inserts of the class
124 to call validate() before actually executing.
130 Validates all the data in the object against the pre-defined validation
131 module and profile. If there is a problem then a hard error will be
132 thrown. If you put the validation in an eval you can capture whatever
133 the module's check() method returned.
137 __PACKAGE__->auto_validate( 0 );
139 Turns on and off auto-validation. This feature makes all UPDATEs and
140 INSERTs call the validate() method before doing anything. The default
141 is for auto-validation to be on.
147 Aran C. Deltac <bluefeet@cpan.org>
151 You may distribute this code under the same terms as Perl itself.