POD::Coverage additions
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Validation.pm
CommitLineData
86e6ac3a 1package DBIx::Class::Validation;
2
3use strict;
4use warnings;
5
6use base qw( DBIx::Class );
86e6ac3a 7use English qw( -no_match_vars );
8
54540863 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.
86e6ac3a 11
12our $VERSION = '0.01';
13
14__PACKAGE__->mk_classdata( 'validation_module' => 'FormValidator::Simple' );
15__PACKAGE__->mk_classdata( 'validation_profile' );
16__PACKAGE__->mk_classdata( 'validation_auto' => 1 );
17
18sub validation_module {
19 my $class = shift;
20 my $module = shift;
21
22 eval("use $module");
701da8c4 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'));
86e6ac3a 25
26 $class->_validation_module_accessor( $module );
27}
28
29sub validation {
30 my $class = shift;
31 my %args = @_;
32
33 $class->validation_module( $args{module} ) if (exists $args{module});
34 $class->validation_profile( $args{profile} ) if (exists $args{profile});
e430cbeb 35 $class->validation_auto( $args{auto} ) if (exists $args{auto});
86e6ac3a 36}
37
38sub validate {
39 my $self = shift;
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());
701da8c4 45 $self->throw_exception( $result );
86e6ac3a 46}
47
48sub insert {
49 my $self = shift;
50 $self->validate if ($self->validation_auto());
51 $self->next::method(@_);
52}
53
54sub update {
55 my $self = shift;
56 $self->validate if ($self->validation_auto());
57 $self->next::method(@_);
58}
59
601;
61__END__
62
63=head1 NAME
64
65DBIx::Class::Validation - Validate all data before submitting to your database.
66
67=head1 SYNOPSIS
68
69In your base DBIC package:
70
71 __PACKAGE__->load_components(qw/... Validation/);
72
73And in your subclasses:
74
75 __PACKAGE__->validation(
76 module => 'FormValidator::Simple',
77 profile => { ... },
78 auto => 1,
79 );
80
81And then somewhere else:
82
83 eval{ $obj->validate() };
84 if( my $results = $EVAL_ERROR ){
85 ...
86 }
87
88=head1 METHODS
89
90=head2 validation
91
92 __PACKAGE__->validation(
93 module => 'FormValidator::Simple',
94 profile => { ... },
95 auto => 1,
96 );
97
75d07914 98Calls validation_module(), validation_profile(), and validation_auto() if the corresponding
86e6ac3a 99argument is defined.
100
101=head2 validation_module
102
103 __PACKAGE__->validation_module('Data::FormValidator');
104
75d07914 105Sets the validation module to use. Any module that supports a check() method just like
86e6ac3a 106Data::FormValidator's can be used here, such as FormValidator::Simple.
107
108Defaults to FormValidator::Simple.
109
110=head2 validation_profile
111
112 __PACKAGE__->validation_profile(
113 { ... }
114 );
115
116Sets the profile that will be passed to the validation module.
117
118=head2 validation_auto
119
120 __PACKAGE__->validation_auto( 1 );
121
75d07914 122This flag, when enabled, causes any updates or inserts of the class
86e6ac3a 123to call validate() before actually executing.
124
125=head2 validate
126
127 $obj->validate();
128
75d07914 129Validates all the data in the object against the pre-defined validation
130module and profile. If there is a problem then a hard error will be
131thrown. If you put the validation in an eval you can capture whatever
86e6ac3a 132the module's check() method returned.
133
134=head2 auto_validate
135
136 __PACKAGE__->auto_validate( 0 );
137
75d07914 138Turns on and off auto-validation. This feature makes all UPDATEs and
139INSERTs call the validate() method before doing anything. The default
86e6ac3a 140is for auto-validation to be on.
141
142Defaults to on.
143
9b83fccd 144=head1 EXTENDED METHODS
145
146The following methods are extended by this module:-
147
148=over 4
149
150=item insert
151
152=item update
153
154=back
155
86e6ac3a 156=head1 AUTHOR
157
158Aran C. Deltac <bluefeet@cpan.org>
159
160=head1 LICENSE
161
162You may distribute this code under the same terms as Perl itself.
163