1 package DBIx::Class::Validation;
6 use base qw( DBIx::Class );
7 use English qw( -no_match_vars );
9 #local $^W = 0; # Silence C:D:I redefined sub errors.
10 # Switched to C::D::Accessor which doesn't do this. Hate hate hate hate.
12 our $VERSION = '0.01';
14 __PACKAGE__->mk_classdata( 'validation_module' => 'FormValidator::Simple' );
15 __PACKAGE__->mk_classdata( 'validation_profile' );
16 __PACKAGE__->mk_classdata( 'validation_auto' => 1 );
18 sub validation_module {
23 $class->throw_exception("Unable to load the validation module '$module' because $EVAL_ERROR") if ($EVAL_ERROR);
24 $class->throw_exception("The '$module' module does not support the check method") if (!$module->can('check'));
26 $class->_validation_module_accessor( $module );
33 $class->validation_module( $args{module} ) if (exists $args{module});
34 $class->validation_profile( $args{profile} ) if (exists $args{profile});
35 $class->validation_auto( $args{auto} ) if (exists $args{auto});
40 my %data = $self->get_columns();
41 my $module = $self->validation_module();
42 my $profile = $self->validation_profile();
43 my $result = $module->check( \%data => $profile );
44 return $result if ($result->success());
45 $self->throw_exception( $result );
50 $self->validate if ($self->validation_auto());
51 $self->next::method(@_);
56 $self->validate if ($self->validation_auto());
57 $self->next::method(@_);
65 DBIx::Class::Validation - Validate all data before submitting to your database.
69 In your base DBIC package:
71 __PACKAGE__->load_components(qw/... Validation/);
73 And in your subclasses:
75 __PACKAGE__->validation(
76 module => 'FormValidator::Simple',
81 And then somewhere else:
83 eval{ $obj->validate() };
84 if( my $results = $EVAL_ERROR ){
92 __PACKAGE__->validation(
93 module => 'FormValidator::Simple',
98 Calls validation_module(), validation_profile(), and validation_auto() if the corresponding
101 =head2 validation_module
103 __PACKAGE__->validation_module('Data::FormValidator');
105 Sets the validation module to use. Any module that supports a check() method just like
106 Data::FormValidator's can be used here, such as FormValidator::Simple.
108 Defaults to FormValidator::Simple.
110 =head2 validation_profile
112 __PACKAGE__->validation_profile(
116 Sets the profile that will be passed to the validation module.
118 =head2 validation_auto
120 __PACKAGE__->validation_auto( 1 );
122 This flag, when enabled, causes any updates or inserts of the class
123 to call validate() before actually executing.
129 Validates all the data in the object against the pre-defined validation
130 module and profile. If there is a problem then a hard error will be
131 thrown. If you put the validation in an eval you can capture whatever
132 the module's check() method returned.
136 __PACKAGE__->auto_validate( 0 );
138 Turns on and off auto-validation. This feature makes all UPDATEs and
139 INSERTs call the validate() method before doing anything. The default
140 is for auto-validation to be on.
146 Aran C. Deltac <bluefeet@cpan.org>
150 You may distribute this code under the same terms as Perl itself.