r1735 - in packages/libformvalidator-simple-perl/branches/upstream/current: . lib/FormValidator lib/FormValidator/Simple t

Krzysztof Krzyzaniak eloy at costa.debian.org
Fri Dec 23 22:14:55 UTC 2005


Author: eloy
Date: 2005-12-23 22:14:51 +0000 (Fri, 23 Dec 2005)
New Revision: 1735

Added:
   packages/libformvalidator-simple-perl/branches/upstream/current/t/25_all.t
Modified:
   packages/libformvalidator-simple-perl/branches/upstream/current/Changes
   packages/libformvalidator-simple-perl/branches/upstream/current/MANIFEST
   packages/libformvalidator-simple-perl/branches/upstream/current/META.yml
   packages/libformvalidator-simple-perl/branches/upstream/current/lib/FormValidator/Simple.pm
   packages/libformvalidator-simple-perl/branches/upstream/current/lib/FormValidator/Simple/Messages.pm
   packages/libformvalidator-simple-perl/branches/upstream/current/lib/FormValidator/Simple/Validator.pm
   packages/libformvalidator-simple-perl/branches/upstream/current/t/22_messages.t
   packages/libformvalidator-simple-perl/branches/upstream/current/t/23_messages_yaml.t
Log:
Load /tmp/tmp.dBOLwT/libformvalidator-simple-perl-0.13 into
packages/libformvalidator-simple-perl/branches/upstream/current.


Modified: packages/libformvalidator-simple-perl/branches/upstream/current/Changes
===================================================================
--- packages/libformvalidator-simple-perl/branches/upstream/current/Changes	2005-12-23 22:08:35 UTC (rev 1734)
+++ packages/libformvalidator-simple-perl/branches/upstream/current/Changes	2005-12-23 22:14:51 UTC (rev 1735)
@@ -1,5 +1,12 @@
 Revision history for Perl extension FormValidator::Simple.
 
+0.13  Thu Nov 24 00:45:00 2005
+    - added set_message_format
+      Thanks to Daisuke Murase.
+
+0.12  Thu Nov 24 00:45:00 2005
+    - added validation ALL
+
 0.11  Thu Nov 24 00:45:00 2005
     - added set_option method
 

Modified: packages/libformvalidator-simple-perl/branches/upstream/current/MANIFEST
===================================================================
--- packages/libformvalidator-simple-perl/branches/upstream/current/MANIFEST	2005-12-23 22:08:35 UTC (rev 1734)
+++ packages/libformvalidator-simple-perl/branches/upstream/current/MANIFEST	2005-12-23 22:14:51 UTC (rev 1735)
@@ -41,5 +41,6 @@
 t/22_messages.t
 t/23_messages_yaml.t
 t/24_options.t
+t/25_all.t
 t/conf/messages.yml
 t/lib/FormValidator/Simple/Plugin/Sample.pm

Modified: packages/libformvalidator-simple-perl/branches/upstream/current/META.yml
===================================================================
--- packages/libformvalidator-simple-perl/branches/upstream/current/META.yml	2005-12-23 22:08:35 UTC (rev 1734)
+++ packages/libformvalidator-simple-perl/branches/upstream/current/META.yml	2005-12-23 22:14:51 UTC (rev 1735)
@@ -1,7 +1,7 @@
 # http://module-build.sourceforge.net/META-spec.html
 #XXXXXXX This is a prototype!!!  It will change in the future!!! XXXXX#
 name:         FormValidator-Simple
-version:      0.11
+version:      0.13
 version_from: lib/FormValidator/Simple.pm
 installdirs:  site
 requires:

Modified: packages/libformvalidator-simple-perl/branches/upstream/current/lib/FormValidator/Simple/Messages.pm
===================================================================
--- packages/libformvalidator-simple-perl/branches/upstream/current/lib/FormValidator/Simple/Messages.pm	2005-12-23 22:08:35 UTC (rev 1734)
+++ packages/libformvalidator-simple-perl/branches/upstream/current/lib/FormValidator/Simple/Messages.pm	2005-12-23 22:14:51 UTC (rev 1735)
@@ -6,11 +6,20 @@
 sub new {
     my $class = shift;
     my $self  = bless {
-        _data => undef,
+        _data   => undef,
+        _format => "%s",
     }, $class;
     return $self;
 }
 
+sub format {
+    my ($self, $format) = @_;
+    if ($format) {
+        $self->{_format} = $format;
+    }
+    $self->{_format};
+}
+
 sub load {
     my ($self, $data) = @_;
     if (ref $data eq 'HASH') {
@@ -34,15 +43,38 @@
 }
 
 sub get {
+    my $self = shift;
+    my $msg  = $self->_get(@_);
+    return sprintf $self->format, $msg;
+}
+
+sub _get {
     my ($self, $action, $name, $type) = @_;
     my $data = $self->{_data};
     unless ($data) {
-    FormValidator::Simple::Exception->throw(
-    qq/set messages before calling get()./
-    );
+        FormValidator::Simple::Exception->throw(
+        qq/set messages before calling get()./
+        );
     }
-    unless ( exists $data->{$action} ) {
-        return "$name is invalid.";
+
+    unless ( $action && exists $data->{$action} ) {
+        if ( exists $data->{DEFAULT} ) {
+            if ( exists $data->{DEFAULT}{$name} ) {
+                my $conf = $data->{DEFAULT}{$name};
+                if ( exists $conf->{$type} ) {
+                    return $conf->{$type};
+                }
+                elsif ( exists $conf->{DEFAULT} ) {
+                    return $conf->{DEFAULT};
+                }
+            }
+            else {
+                return "$name is invalid.";
+            }
+        }
+        else {
+            return "$name is invalid.";
+        }
     }
     if ( exists $data->{$action}{$name} ) {
         my $conf = $data->{$action}{$name};
@@ -52,7 +84,27 @@
         elsif ( exists $conf->{DEFAULT} ) {
             return $conf->{DEFAULT};
         }
+        elsif ( exists  $data->{DEFAULT}
+              && exists $data->{DEFAULT}{$name} ) {
+            my $conf = $data->{DEFAULT}{$name};
+            if ( exists $conf->{$type} ) {
+                return $conf->{$type};
+            }
+            elsif ( exists $conf->{DEFAULT} ) {
+                return $conf->{DEFAULT};
+            }
+        }
     }
+    elsif ( exists $data->{DEFAULT}
+         && exists $data->{DEFAULT}{$name} ) {
+        my $conf = $data->{DEFAULT}{$name};
+        if ( exists $conf->{$type} ) {
+            return $conf->{$type};
+        }
+        elsif ( exists $conf->{DEFAULT} ) {
+            return $conf->{DEFAULT};
+        }
+    }
     return "$name is invalid.";
 }
 

Modified: packages/libformvalidator-simple-perl/branches/upstream/current/lib/FormValidator/Simple/Validator.pm
===================================================================
--- packages/libformvalidator-simple-perl/branches/upstream/current/lib/FormValidator/Simple/Validator.pm	2005-12-23 22:08:35 UTC (rev 1734)
+++ packages/libformvalidator-simple-perl/branches/upstream/current/lib/FormValidator/Simple/Validator.pm	2005-12-23 22:14:51 UTC (rev 1735)
@@ -291,6 +291,16 @@
     return $data =~ /$reg/ ? TRUE : FALSE;
 }
 
+sub ALL {
+    my ($self, $params, $args) = @_;
+    foreach my $param ( @$params ) {
+        unless ( defined $param && $param ne '' ) {
+            return FALSE;
+        }
+    }
+    return TRUE;
+}
+
 1;
 __END__
 

Modified: packages/libformvalidator-simple-perl/branches/upstream/current/lib/FormValidator/Simple.pm
===================================================================
--- packages/libformvalidator-simple-perl/branches/upstream/current/lib/FormValidator/Simple.pm	2005-12-23 22:08:35 UTC (rev 1734)
+++ packages/libformvalidator-simple-perl/branches/upstream/current/lib/FormValidator/Simple.pm	2005-12-23 22:14:51 UTC (rev 1735)
@@ -11,7 +11,7 @@
 use FormValidator::Simple::Constants;
 use FormValidator::Simple::Messages;
 
-our $VERSION = '0.11';
+our $VERSION = '0.13';
 
 __PACKAGE__->mk_accessors(qw/data prof results/);
 
@@ -58,6 +58,13 @@
     $class->messages->load($file);
 }
 
+sub set_message_format {
+    my ($proto, $format) = @_;
+    $format ||= '';
+    my $class = ref $proto || $proto;
+    $class->messages->format($format);
+}
+
 sub new {
     my $proto = shift;
     my $class = ref $proto || $proto;
@@ -227,7 +234,7 @@
 
 A hash reference includes input data, or an object of some class that has a method named 'param', for example L<CGI>, is needed as first argument.
 
-And set profile as array reference into second argument. Profile confists of some pairs of input data and constraints.
+And set profile as array reference into second argument. Profile consists of some pairs of input data and constraints.
 
     my $q = CGI->new;
     $q->param( param1 => 'hoge' );
@@ -539,13 +546,19 @@
 or a YAML file.
 
     # messages.yml
+    DEFAULT:
+        name:
+            DEFAULT: name is invalid!
     action1:
         name:
             NOT_BLANK: input name!
             LENGTH: input name(length should be between 0 and 10)!
         email:
             DEFAULT: input correct email address!
-
+    action2:
+        name:
+            DEFAULT: ...
+            
     # in your perl-script, set the file's path.
     FormValidator::Simple->set_messages('messages.yml');
 
@@ -566,6 +579,7 @@
         print $message, "\n";
     }
 
+When it can't find indicated action, name, and type, it searches proper message from DEFAULT action.
 If in template file,
 
     [% IF result.has_error %]
@@ -574,6 +588,17 @@
         [% END %]
     [% END %]
 
+you can set each message format.
+
+    FormValidator::Simple->set_message_format('<p>%s</p>');
+    my $result = FormValidator::Simple->check( $q => [
+        ...profile
+    ] );
+
+    [% IF result.has_error %]
+        [% result.messages('action1').join("\n") %]
+    [% END %]
+
 =head1 RESULT HANDLING
 
 See L<FormValidator::Simple::Results>

Modified: packages/libformvalidator-simple-perl/branches/upstream/current/t/22_messages.t
===================================================================
--- packages/libformvalidator-simple-perl/branches/upstream/current/t/22_messages.t	2005-12-23 22:08:35 UTC (rev 1734)
+++ packages/libformvalidator-simple-perl/branches/upstream/current/t/22_messages.t	2005-12-23 22:14:51 UTC (rev 1735)
@@ -1,9 +1,14 @@
 use strict;
-use Test::More tests => 5;
+use Test::More tests => 6;
 BEGIN{ use_ok("FormValidator::Simple") }
 use CGI;
 
 FormValidator::Simple->set_messages( {
+    DEFAULT => {
+        data4 => {
+            DEFAULT => 'input data4',    
+        },
+    },
     test => {
         data1 => {
             NOT_BLANK => 'input data1',
@@ -23,11 +28,13 @@
 $q->param( data1 => 'hoge' );
 $q->param( data2 => '123'  );
 $q->param( data3 => ''     );
+$q->param( data4 => ''     );
 
 my $r = FormValidator::Simple->check( $q => [
     data1 => [qw/NOT_BLANK INT/, [qw/LENGTH 0 3/] ],
     data2 => [qw/NOT_BLANK ASCII/, [qw/LENGTH 5/]],
     data3 => [qw/NOT_BLANK/], 
+    data4 => [qw/NOT_BLANK/],
 ] );
 
 my $messages = $r->messages('test');
@@ -35,4 +42,5 @@
 is($messages->[1], 'data1 has wrong length');
 is($messages->[2], 'default error for data2');
 is($messages->[3], 'input data3');
+is($messages->[4], 'input data4');
 

Modified: packages/libformvalidator-simple-perl/branches/upstream/current/t/23_messages_yaml.t
===================================================================
--- packages/libformvalidator-simple-perl/branches/upstream/current/t/23_messages_yaml.t	2005-12-23 22:08:35 UTC (rev 1734)
+++ packages/libformvalidator-simple-perl/branches/upstream/current/t/23_messages_yaml.t	2005-12-23 22:14:51 UTC (rev 1735)
@@ -1,5 +1,5 @@
 use strict;
-use Test::More tests => 5;
+use Test::More tests => 9;
 BEGIN{ use_ok("FormValidator::Simple") }
 use CGI;
 my $conf_file = "t/conf/messages.yml";
@@ -22,3 +22,11 @@
 is($messages->[2], 'default error for data2');
 is($messages->[3], 'input data3');
 
+
+FormValidator::Simple->set_message_format('<p>%s</p>');
+my $messages2 = $r->messages('test');
+is($messages2->[0], '<p>input integer for data1</p>');
+is($messages2->[1], '<p>data1 has wrong length</p>');
+is($messages2->[2], '<p>default error for data2</p>');
+is($messages2->[3], '<p>input data3</p>');
+

Added: packages/libformvalidator-simple-perl/branches/upstream/current/t/25_all.t
===================================================================
--- packages/libformvalidator-simple-perl/branches/upstream/current/t/25_all.t	2005-12-23 22:08:35 UTC (rev 1734)
+++ packages/libformvalidator-simple-perl/branches/upstream/current/t/25_all.t	2005-12-23 22:14:51 UTC (rev 1735)
@@ -0,0 +1,19 @@
+use strict;
+use Test::More tests => 3;
+
+BEGIN{ use_ok("FormValidator::Simple") }
+
+use CGI;
+
+my $q = CGI->new;
+$q->param( foo => 'foo' );
+$q->param( bar => 'bar' );
+$q->param( buz => '' );
+
+my $r = FormValidator::Simple->check( $q => [ 
+  { all1 => [qw/foo bar/] } => ['ALL'],
+  { all2 => [qw/bar buz/] } => ['ALL']
+] );
+
+ok(!$r->invalid('all1'));
+ok($r->invalid('all2'));




More information about the Pkg-perl-cvs-commits mailing list