]> Dogcows Code - chaz/p5-CGI-Ex/blobdiff - lib/CGI/Ex/Validate.pm
CGI::Ex 2.08
[chaz/p5-CGI-Ex] / lib / CGI / Ex / Validate.pm
index b0c6b4f5ff6ee4e74862c29faa5f2c5090203489..5bb62e3f362c97540e66bfcaf4c60db2a4d797d6 100644 (file)
@@ -7,7 +7,7 @@ CGI::Ex::Validate - another form validator - but it does javascript in parallel
 =cut
 
 ###----------------------------------------------------------------###
-#  Copyright 2006 - Paul Seamons                                     #
+#  Copyright 2007 - Paul Seamons                                     #
 #  Distributed under the Perl Artistic License without warranty      #
 ###----------------------------------------------------------------###
 
@@ -22,7 +22,7 @@ use vars qw($VERSION
             @UNSUPPORTED_BROWSERS
             );
 
-$VERSION = '2.02';
+$VERSION = '2.08';
 
 $DEFAULT_EXT   = 'val';
 $QR_EXTRA      = qr/^(\w+_error|as_(array|string|hash)_\w+|no_\w+)/;
@@ -126,6 +126,7 @@ sub validate {
       next if $found{$field};
       my $field_val = $group_val->{$field};
       die "Found a nonhashref value on field $field" if ! UNIVERSAL::isa($field_val, 'HASH');
+      $field_val->{'field'} = $field if ! defined $field_val->{'field'};
       push @$fields, $field_val;
     }
 
@@ -302,6 +303,11 @@ sub validate_buddy {
       $value =~ s/\s+$//;
       $modified = 1;
     }
+    if ($field_val->{'trim_control_chars'}) {
+      $value =~ y/\t/ /;
+      $value =~ y/\x00-\x1F//d;
+      $modified = 1;
+    }
     if ($field_val->{'to_upper_case'}) { # uppercase
       $value = uc($value);
       $modified = 1;
@@ -334,6 +340,7 @@ sub validate_buddy {
         }
       }else{
         foreach my $value (@$values) {
+          next if ! defined $value;
           $value =~ s{(?$opt:$pat)}{
             my @match = (undef, $1, $2, $3, $4, $5, $6); # limit on the number of matches
             my $copy = $swap;
@@ -795,7 +802,16 @@ sub generate_js {
         "$js_uri_path/CGI/Ex/validate.js";
     };
 
-    if (eval { require JSON }) {
+    if (! $self->{'no_jsondump'} && eval { require CGI::Ex::JSONDump }) {
+        my $json = CGI::Ex::JSONDump->new({pretty => 1})->dump($val_hash);
+        return qq{<script src="$js_uri_path_validate"></script>
+<script>
+document.validation = $json;
+if (document.check_form) document.check_form("$form_name");
+</script>
+};
+
+    } elsif (! $self->{'no_json'} && eval { require JSON }) {
         my $json = JSON->new(pretty => 1)->objToJson($val_hash);
 
         return qq{<script src="$js_uri_path_validate"></script>
@@ -1711,6 +1727,13 @@ not trim.
 
     {field => 'foo', do_not_trim => 1}
 
+=item C<trim_control_chars>
+
+Off by default.  If set to true, removes characters in the
+\x00 to \x31 range (Tabs are translated to a single space).
+
+    {field => 'foo', trim_control_chars => 1}
+
 =item C<replace>
 
 Pass a swap pattern to change the actual value of the form.
This page took 0.020193 seconds and 4 git commands to generate.