[Bast-commits] r3312 - in trunk/DBIx-Class-Validation: .
lib/DBIx/Class t t/lib/DBIC t/lib/DBIC/Test/Schema t/sql
claco at dev.catalyst.perl.org
claco at dev.catalyst.perl.org
Sun May 13 01:54:43 GMT 2007
Author: claco
Date: 2007-05-13 01:54:41 +0100 (Sun, 13 May 2007)
New Revision: 3312
Modified:
trunk/DBIx-Class-Validation/Changes
trunk/DBIx-Class-Validation/lib/DBIx/Class/Validation.pm
trunk/DBIx-Class-Validation/t/dfv.t
trunk/DBIx-Class-Validation/t/lib/DBIC/Test.pm
trunk/DBIx-Class-Validation/t/lib/DBIC/Test/Schema/Test.pm
trunk/DBIx-Class-Validation/t/sql/test.sqlite.sql
Log:
Use column accessors to set columns instead of set_column (John Napiorkowski)
Modified: trunk/DBIx-Class-Validation/Changes
===================================================================
--- trunk/DBIx-Class-Validation/Changes 2007-05-12 20:22:09 UTC (rev 3311)
+++ trunk/DBIx-Class-Validation/Changes 2007-05-13 00:54:41 UTC (rev 3312)
@@ -1,5 +1,9 @@
Revision history for DBIx::Class::Validation
+0.01004 Sat May 12 10:45:23 2007
+ - Use column accessors to set columns instead of set_column
+ (John Napiorkowski)
+
0.01003 Tue May 08 18:49:11 2007
- Fixed DBIC::Test comment snafu
- Profiles can now be a subref that will dynamically generate the
Modified: trunk/DBIx-Class-Validation/lib/DBIx/Class/Validation.pm
===================================================================
--- trunk/DBIx-Class-Validation/lib/DBIx/Class/Validation.pm 2007-05-12 20:22:09 UTC (rev 3311)
+++ trunk/DBIx-Class-Validation/lib/DBIx/Class/Validation.pm 2007-05-13 00:54:41 UTC (rev 3312)
@@ -1,25 +1,23 @@
+# $Id$
package DBIx::Class::Validation;
-
use strict;
use warnings;
-use base qw( DBIx::Class );
-use English qw( -no_match_vars );
-use FormValidator::Simple 0.17;
+BEGIN {
+ use base qw/DBIx::Class/;
+ use English qw/-no_match_vars/;
+ use FormValidator::Simple 0.17;
+};
-#local $^W = 0; # Silence C:D:I redefined sub errors.
-# Switched to C::D::Accessor which doesn't do this. Hate hate hate hate.
+our $VERSION = '0.01004';
-our $VERSION = '0.01003';
+__PACKAGE__->mk_classdata('validation_profile');
+__PACKAGE__->mk_classdata('validation_auto' => 1);
+__PACKAGE__->mk_classdata('validation_filter' => 0);
+__PACKAGE__->mk_classdata('_validation_module_accessor');
+__PACKAGE__->validation_module('FormValidator::Simple');
-__PACKAGE__->mk_classdata( 'validation_profile' );
-__PACKAGE__->mk_classdata( 'validation_auto' => 1 );
-__PACKAGE__->mk_classdata( 'validation_filter' => 0 );
-__PACKAGE__->mk_classdata( '_validation_module_accessor' );
-__PACKAGE__->validation_module( 'FormValidator::Simple' );
-
-
=head1 NAME
DBIx::Class::Validation - Validate all data before submitting to your database.
@@ -63,15 +61,14 @@
=cut
sub validation {
- my $self = shift;
- my %args = @_;
-
- $self->validation_module( $args{module} ) if exists $args{module};
- $self->validation_profile( $args{profile} ) if exists $args{profile};
- $self->validation_auto( $args{auto} ) if exists $args{auto};
- $self->validation_filter( $args{filter} ) if exists $args{filter};
-}
+ my ($self, %args) = @_;
+ $self->validation_module($args{module}) if exists $args{module};
+ $self->validation_profile($args{profile}) if exists $args{profile};
+ $self->validation_auto($args{auto}) if exists $args{auto};
+ $self->validation_filter($args{filter}) if exists $args{filter};
+};
+
=head2 validation_module
__PACKAGE__->validation_module('Data::FormValidator');
@@ -98,7 +95,7 @@
};
return ref $self->_validation_module_accessor;
-}
+};
=head2 validation_profile
@@ -117,9 +114,9 @@
my $profile = sub {
my $result = shift @_;
- return {`
+ return {
required => [qw/email/],
- constraint_methods => {
+ constraint_methods => {
email => sub {
my ($dvf, $val) = @_;
return $result->result_source->resultset->find({email=>$val}) ? 0:1;
@@ -171,17 +168,21 @@
my %data = $self->get_columns;
my $module = $self->validation_module;
my $profile = $self->validation_profile;
+
+ if (ref $profile eq 'CODE') {
+ $profile = $profile->($self);
+ };
my $result = $module->check( \%data => $profile );
if ($result->success) {
if ($self->validation_filter && $result->can('valid')) {
- $self->set_column($_, $result->valid($_)) for ($result->valid);
- }
+ $self->$_($result->valid($_)) for ($result->valid);
+ };
return $result;
} else {
$self->throw_exception($result);
};
-}
+};
=head1 EXTENDED METHODS
@@ -207,7 +208,7 @@
my $self = shift;
$self->validate if $self->validation_auto;
$self->next::method(@_);
-}
+};
1;
__END__
Modified: trunk/DBIx-Class-Validation/t/dfv.t
===================================================================
--- trunk/DBIx-Class-Validation/t/dfv.t 2007-05-12 20:22:09 UTC (rev 3311)
+++ trunk/DBIx-Class-Validation/t/dfv.t 2007-05-13 00:54:41 UTC (rev 3312)
@@ -10,7 +10,7 @@
plan skip_all => 'Data::FormValidator not installed'
unless eval 'require Data::FormValidator';
- plan tests => 5;
+ plan tests => 10;
};
use Data::FormValidator::Constraints qw(:closures);
@@ -50,3 +50,53 @@
Class::C3->reinitialize();
$row = eval{ $schema->resultset('Test')->create({name => 'test', email => 'test at test.org'}) };
is $row->name, 'test', 'no filters applied';
+
+
+SKIP: {
+ skip 'DateTime not installed', 5 unless eval 'require DateTime';
+
+ ## Create a profile that checks to make sure the email is unique in the database
+
+ my $dt = DateTime->new(year=>2007);
+ my $test_time = $dt->epoch;
+
+ my $profile = sub {
+ my $result = shift @_;
+ return {
+ required => [qw/email createts/],
+ field_filters => {
+ createts => sub {
+ ## We assume that stuff comes in epoch (seconds from 1970)
+ return DateTime->from_epoch( epoch => shift );
+ },
+ },
+ constraint_methods => {
+ email => sub {
+ my ($dvf, $value) = @_;
+ $dvf->name_this('email_not_unique');
+ return $result->result_source->resultset->find({email=>$value}) ? 0 : 1;
+ },
+ },
+ };
+ };
+
+ ## Reset the profile
+ DBIC::Test::Schema::Test->validation_profile($profile);
+ DBIC::Test::Schema::Test->validation_filter(1);
+ Class::C3->reinitialize();
+
+ ## Create a new row with a new email.
+
+ my $new_email_rs = $schema->resultset('Test')->create({name => 'testA', email => 'testaa at test.org', 'createts'=> $test_time});
+ is $new_email_rs->email, 'testaa at test.org', 'Created a unique Email Address';
+
+ my $bad_rs = eval{ $schema->resultset('Test')->create({name => 'testA', email => 'testaa at test.org', 'createts'=> $test_time}) };
+ isa_ok $@, 'Data::FormValidator::Results', 'Failed as expected';
+
+ my @bad_fields = $@->invalid;
+ my $errs_msgs = $@->invalid;
+
+ ok($bad_fields[0] eq 'email', 'Invalid Field correctly identified');
+ ok($errs_msgs->{email}->[0] eq 'email_not_unique', 'Invalid Field Message Found');
+ ok($new_email_rs->createts->epoch == $test_time, "Correctly filtered inflated object");
+};
\ No newline at end of file
Modified: trunk/DBIx-Class-Validation/t/lib/DBIC/Test/Schema/Test.pm
===================================================================
--- trunk/DBIx-Class-Validation/t/lib/DBIC/Test/Schema/Test.pm 2007-05-12 20:22:09 UTC (rev 3311)
+++ trunk/DBIx-Class-Validation/t/lib/DBIC/Test/Schema/Test.pm 2007-05-13 00:54:41 UTC (rev 3312)
@@ -7,7 +7,7 @@
use base qw/DBIx::Class::Core/;
};
-__PACKAGE__->load_components(qw/Validation PK::Auto Core/);
+__PACKAGE__->load_components(qw/Validation InflateColumn::DateTime PK::Auto Core/);
__PACKAGE__->table('test');
__PACKAGE__->add_columns(
'id' => {
@@ -24,7 +24,12 @@
data_type => 'varchar',
size => 100,
is_nullable => 1,
- }
+ },
+ 'createts' => {
+ data_type => 'timestamp',
+ size => 14,
+ is_nullable => 1,
+ },
);
__PACKAGE__->set_primary_key('id');
Modified: trunk/DBIx-Class-Validation/t/lib/DBIC/Test.pm
===================================================================
--- trunk/DBIx-Class-Validation/t/lib/DBIC/Test.pm 2007-05-12 20:22:09 UTC (rev 3311)
+++ trunk/DBIx-Class-Validation/t/lib/DBIC/Test.pm 2007-05-13 00:54:41 UTC (rev 3312)
@@ -44,7 +44,7 @@
mkdir($db_dir) unless -d $db_dir;
my $dsn = 'dbi:SQLite:' . $db;
- my $schema = DBIC::Test::Schema->compose_namespace($namespace)->connect($dsn);
+ my $schema = DBIC::Test::Schema->compose_namespace($namespace)->connect($dsn, undef, undef, {AutoCommit => 1});
$schema->storage->on_connect_do([
'PRAGMA synchronous = OFF',
'PRAGMA temp_store = MEMORY'
Modified: trunk/DBIx-Class-Validation/t/sql/test.sqlite.sql
===================================================================
--- trunk/DBIx-Class-Validation/t/sql/test.sqlite.sql 2007-05-12 20:22:09 UTC (rev 3311)
+++ trunk/DBIx-Class-Validation/t/sql/test.sqlite.sql 2007-05-13 00:54:41 UTC (rev 3312)
@@ -1,5 +1,6 @@
CREATE TABLE test (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name VARCHAR(100) NULL,
- email VARCHAR(100) NULL
+ email VARCHAR(100) NULL,
+ createts TIMESTAMP(14) NULL
);
More information about the Bast-commits
mailing list