Skip Menu |
 

This queue is for tickets about the DBIx-Class-Validation CPAN distribution.

Report information
The Basics
Id: 86402
Status: new
Priority: 0/
Queue: DBIx-Class-Validation

People
Owner: Nobody in particular
Requestors: KWAKWA [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.02005
Fixed in: (no value)

Attachments


Subject: [PATCH] adding FormValidator::Simple validation_filter support
Download (untitled) / with headers
text/plain 413b
First time RT patcher, so I hope I've done this properly. DBIC::Validation includes FormValidator::Simple by default, but doesn't support the use of validation_filter with it because its valid() method returns a HASHREF rather than an ARRAYREF like Data::FormValidator. I've provided the simple fix, complete with tests. Cheers, -kwa P.s. If I've done something wrong, I'd love to know so I can put it right.
Subject: fvs_validation_filter.patch
diff --git a/Changes b/Changes index de850a4..6af52b3 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for DBIx::Class::Validation +0.02006 Tue Jun 25 10:51:55 2013 + - Added validation_filter support for FormValidator::Simple (KWAKWA) + 0.02005 Sat Jul 11 11:20:03 2009 - Fix bug that skipped validation when setting values while doing an update() like this: $row->update($hashref) (Sergio Salvi) (RT#47709) diff --git a/MANIFEST b/MANIFEST index a0bb78f..8c6a5e2 100644 --- a/MANIFEST +++ b/MANIFEST @@ -21,6 +21,7 @@ t/lib/DBIC/Test/Schema.pm t/lib/DBIC/Test/Schema/Test.pm t/lib/Validator.pm t/lib/ValidatorWithoutCheck.pm +t/lib/FormValidator/Simple/Plugin/FilterTest.pm t/manifest.t t/pod_coverage.t t/pod_spelling.t diff --git a/META.yml b/META.yml index c17ed1b..8f236da 100644 --- a/META.yml +++ b/META.yml @@ -27,4 +27,4 @@ requires: perl: 5.8.1 resources: license: http://dev.perl.org/licenses/ -version: 0.02005 +version: 0.02006 diff --git a/README b/README index 42f5a9c..59c1719 100644 --- a/README +++ b/README @@ -91,7 +91,8 @@ METHODS your validation modules "check" method. This is primarily meant for use with "Data::FormValidator" but may be used with any validation module that returns a results object that supports a "valid()" method just like - "Data::FormValidator::Results". + "Data::FormValidator::Results". (The "valid()" method must return an ARRAY + reference or HASH reference.) Filters modify your data, so use them carefully. diff --git a/lib/DBIx/Class/Validation.pm b/lib/DBIx/Class/Validation.pm index 29f54cf..55c55b0 100644 --- a/lib/DBIx/Class/Validation.pm +++ b/lib/DBIx/Class/Validation.pm @@ -2,7 +2,7 @@ package DBIx::Class::Validation; use strict; use warnings; -our $VERSION = '0.02005'; +our $VERSION = '0.02006'; BEGIN { use base qw/DBIx::Class Class::Accessor::Grouped/; @@ -148,7 +148,8 @@ UPDATEs and INSERTs modify your data to that of the values returned by your validation modules C<check> method. This is primarily meant for use with L<"Data::FormValidator"> but may be used with any validation module that returns a results object that supports a C<valid()> method just -like L<"Data::FormValidator::Results">. +like L<"Data::FormValidator::Results">. (The C<valid()> method must return +an ARRAY reference or HASH reference.) B<Filters modify your data, so use them carefully>. @@ -178,7 +179,8 @@ sub validate { if ($result->success) { if ($self->validation_filter && $result->can('valid')) { - $self->$_($result->valid($_)) for ($result->valid); + $self->$_($result->valid($_)) + for (ref $result->valid eq 'HASH' ? keys %{$result->valid} : $result->valid); }; return $result; } else { @@ -238,6 +240,8 @@ John Napiorkowski <jjn1056@yahoo.com> Sergio Salvi <sergio@developerl.com> +Paul Williams <kwakwa@cpan.org> + =head1 LICENSE You may distribute this code under the same terms as Perl itself. diff --git a/t/fvs.t b/t/fvs.t index 90b1769..7b220e8 100644 --- a/t/fvs.t +++ b/t/fvs.t @@ -5,17 +5,18 @@ use warnings; BEGIN { use lib 't/lib'; - use DBIC::Test tests => 17; + use DBIC::Test tests => 19; } my $schema = DBIC::Test->init_schema; my $row; my $profile = [ - name => [ 'NOT_BLANK', ['LENGTH', 4, 10] ], + name => [ 'NOT_BLANK', ['LENGTH', 4, 10], 'LOWERCASE' ], ]; DBIC::Test::Schema::Test->validation_profile($profile); +DBIC::Test::Schema::Test->validation_module->import(qw/FilterTest/); Class::C3->reinitialize(); $row = eval{ $schema->resultset('Test')->create({name => ''}) }; @@ -47,6 +48,18 @@ Class::C3->reinitialize(); $row = eval{ $schema->resultset('Test')->create({name => 'qwertyqwerty'}) }; is $row->name, 'qwertyqwerty', 'validation is off'; +# validation filter +DBIC::Test::Schema::Test->validation_auto(1); +DBIC::Test::Schema::Test->validation_filter(1); +Class::C3->reinitialize(); +$row = eval{ $schema->resultset('Test')->create({name => 'TEST', email => 'test@test.org'}); }; +is $row->name, 'test', 'filters applied'; + +DBIC::Test::Schema::Test->validation_filter(0); +Class::C3->reinitialize(); +$row = eval{ $schema->resultset('Test')->create({name => 'TEST', email => 'test@test.org'}) }; +is $row->name, 'TEST', 'no filters applied'; + # validation changes all DBIC::Test::Schema::Test->validation( module => 'Validator', diff --git a/t/lib/FormValidator/Simple/Plugin/FilterTest.pm b/t/lib/FormValidator/Simple/Plugin/FilterTest.pm new file mode 100644 index 0000000..45b4739 --- /dev/null +++ b/t/lib/FormValidator/Simple/Plugin/FilterTest.pm @@ -0,0 +1,16 @@ +package FormValidator::Simple::Plugin::FilterTest; +use strict; +use warnings; + +use FormValidator::Simple::Constants; +use FormValidator::Simple::Exception; + +sub LOWERCASE { + my ($self, $params, $args) = @_; + + map { $_ = lc $_ } @$params; + + return TRUE, $#$params ? $params : $params->[0]; +} + +1;


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.