add support for validating times of day
authorTony Cook <tony@develop-help.com>
Wed, 5 Dec 2012 05:30:51 +0000 (16:30 +1100)
committerTony Cook <tony@develop-help.com>
Wed, 5 Dec 2012 05:30:51 +0000 (16:30 +1100)
site/cgi-bin/modules/DevHelp/Validate.pm
t/010-modules/020-validate.t
t/data/known_pod_issues.txt

index 6771c858502f77d8e090322d0e4cf4e75670b028..bc0b023c266baa3c064d622aa97754b1ac6f3f09 100644 (file)
@@ -6,7 +6,7 @@ use vars qw(@EXPORT_OK @ISA);
 @ISA = qw(Exporter);
 use Carp qw(confess);
 
-our $VERSION = "1.004";
+our $VERSION = "1.005";
 
 my %built_ins =
   (
@@ -125,6 +125,10 @@ my %built_ins =
     maxdate => '+1d',
     maxdatemsg => 'The date entered must be in the past',
    },
+   time =>
+   {
+    time => 1,
+   },
    integer =>
    {
     integer => 1,
@@ -463,6 +467,20 @@ sub validate_field {
          }
        }
       }
+      if ($rule->{time}) {
+       require DevHelp::Date;
+       my $msg;
+       if (my ($hour, $min, $sec)
+           = DevHelp::Date::dh_parse_time($data, \$msg)) {
+         # nothing to do here yet, later it will allow limits
+       }
+       else {
+         $errors->{$field} =
+           _make_error($field, $info, $rule,
+                       '$n is not a valid time of day');
+         last RULE;
+       }
+      }
       if ($rule->{confirm}) {
        my $other = $self->param($rule->{confirm});
        unless ($other eq $data) {
@@ -535,7 +553,11 @@ sub validate_field {
       if ($rule->{ref}) {
        my $method = $rule->{method}
          or confess "Missing method in ref rule $rule_name";
-       unless ($rule->{ref}->$method($data)) {
+       my $before = $rule->{before};
+       my @before = defined $before ? ( ref $before ? @$before : split /,/, $before ) : ();
+       my $after = $rule->{after};
+       my @after = defined $after ? ( ref $after ? @$after : split /,/, $after ) : ();
+       unless ($rule->{ref}->$method(@before, $data, @after)) {
          $errors->{$field} = _make_error($field, $info, $rule, 'No such $n');
          last RULE;
        }
@@ -829,6 +851,8 @@ DevHelp::Validate - handy configurable validation, I hope
 
 =head1 DESCRIPTION
 
+Performs simple validation of CGI or hash data.
+
 =head1 RULES PARAMETER
 
 The rules parameter is a hash with 2 keys:
@@ -857,7 +881,7 @@ A short description of the field, for use in error messages.
 =item rules
 
 A hash of rules.  See the rules description under L<CONFIGURED
-VALIDATON>.
+VALIDATION>.
 
 =back
 
@@ -871,9 +895,9 @@ The values of those keys gives the name of a validation rule, a string
 id for internationlization of the field description and a default
 field description, separated by commas.
 
-Each validation rule name has a corresponding section, [Validate Rule
-I<rule-name>], which describes the rule.  Rule names can also refer to
-built-in rules,
+Each validation rule name has a corresponding section,C<< [Validation
+Rule I<rule-name>] >>, which describes the rule.  Rule names can also
+refer to built-in rules,
 
 Values in the validation rule section are:
 
@@ -918,6 +942,11 @@ letter day of week abbreviations to require the date be only on those
 days of week.  Uses C<dowmsg> from the field or rule for the error
 message.
 
+=item time
+
+If true, validates that the value can be parsed by
+L<DevHelp::Date/dh_parse_time()>.
+
 =item confirm
 
 Specify another field that the field must be equal to, intended for
@@ -940,6 +969,12 @@ with the value to check as a parameter.  The value is considered value
 if the result is true.  This is intended for checking the existence of
 objects in a collection.
 
+Optionally C<before> can be an array ref or comma-separated list of
+parameters to supply before the value.
+
+Optionally C<after> can be an array ref or comma-separated list of
+parameters to supply after the value.
+
 =back
 
 =head1 BUILT-IN RULES
@@ -1026,6 +1061,10 @@ past.
 
 A valid date in the future.
 
+=item time
+
+Parses as a time as per dh_parse_time().
+
 =item integer
 
 Any integer.
index 3aa48a7ece226d0293ea12049d1a6e3c3b1197f8..d4da57e3afecd300a51c764b15d32ed8d02a03d6 100644 (file)
@@ -1,6 +1,6 @@
 #!perl -w
 use strict;
-use Test::More tests => 34;
+use Test::More tests => 38;
 
 BEGIN { use_ok('DevHelp::Validate'); }
 
@@ -212,3 +212,19 @@ BEGIN { use_ok('DevHelp::Validate'); }
     }
   }
 }
+
+# times
+{
+  my $val = DevHelp::Validate::Hash->new
+      (
+       fields =>
+       {
+       time => { rules => "time" },
+       }
+      );
+  my %errors;
+  ok($val->validate({time => "10:00"}, \%errors), "simple hh:mm");
+  ok($val->validate({time => "2pm" }, \%errors), "simple Hpm");
+  ok($val->validate({time => "12pm" }, \%errors), "simple 12pm");
+  ok(!$val->validate({time => "13pm" }, \%errors), "simple 13pm");
+}
index 5d013c565f033886d05e5624ebb174210ee4b3fd..ac0d4ae81b0c6e4565d06fd6d285f65d3ed194de 100644 (file)
@@ -63,13 +63,12 @@ site/cgi-bin/modules/DevHelp/Payments/SecurePay.pm  Verbatim paragraph in NAME se
 site/cgi-bin/modules/DevHelp/Payments/SecurePayXML.pm  Verbatim paragraph in NAME section      1
 site/cgi-bin/modules/DevHelp/Payments/Test.pm  =back without previous =over    1
 site/cgi-bin/modules/DevHelp/Tags/Iterate.pm   =item without previous =over    1
-site/cgi-bin/modules/DevHelp/Validate.pm       empty section in previous paragraph     1
 site/cgi-bin/modules/DevHelp/Validate.pm       multiple occurrence of link target 'confirm'    1
 site/cgi-bin/modules/DevHelp/Validate.pm       multiple occurrence of link target 'date'       1
 site/cgi-bin/modules/DevHelp/Validate.pm       multiple occurrence of link target 'integer'    1
 site/cgi-bin/modules/DevHelp/Validate.pm       multiple occurrence of link target 'required'   1
 site/cgi-bin/modules/DevHelp/Validate.pm       multiple occurrence of link target 'rules'      1
-site/cgi-bin/modules/DevHelp/Validate.pm       unresolved internal link 'CONFIGURED VALIDATON' 1
+site/cgi-bin/modules/DevHelp/Validate.pm       multiple occurrence of link target 'time'       1
 site/cgi-bin/modules/Generate.pm       =item without previous =over    1
 site/cgi-bin/modules/Generate.pm       empty section in previous paragraph     1
 site/cgi-bin/modules/Generate/Article.pm       =item without previous =over    1