logo       
Google Custom Search
    AddThis Social Bookmark Button
-->

Re: [FormBuilder] validate + commas in FB source files: msg#00012

Subject: Re: [FormBuilder] validate + commas in FB source files
        Hello list,

kevin montuori wrote:
> hi all -- first off, CGI::FB is a great module.  it's saved me
> countless hours of tedious mind-numbing typing.  thanks a lot to
> nate and anyone else responsible.

        Same for me. I'm new to it and find it very useful.

> i have a question about using CGI::FormBuilder::Source::File with
> regexps in the validate field: one of the things i really want to do
> is make sure the length of the input is between a min and max value:
> 
>   validate: /^\w{3,32}$/

        I've had the same problem.

 > [...]
> to something like:
> 
>    # split commas
>    my $split_regex = lc $term eq 'validate'
>      ? qr/\s*(?<!\\),\s*/ : qr/\s*,\s*/;
>    @val = split $split_regex, $line;
> 
> which would allow this statement in the source file:
> 
>    validate: /^\w{3\,32}$/
> 
> to produce the more useful:
> 
>   'validate' => '/^\\w{3\\,32}$/'
> 
> it's easy enough to maintain my own source adapter that had this
> particular tweak (and having that hook is damn good thinking) but i
> thought maybe this would be more broadly useful.

        I changed your patch a bit more :

-------------------------------------------------------
# split commas
my $split_regex;
if (lc $term eq 'validate') {
     $split_regex = qr/\s*(?<!\\),\s*/;
} else {
     $split_regex = qr/\s*,\s*/;
}
@val = split $split_regex, $line;

# m=Male, f=Female -> [m,Male], [f,Female] + '\' remove before ','
for (@val) {
     s/\\,/,/g;
     $_ = [ split /\s*=\s*/, $_, 2 ] if /=/;
}
--------------------------------------------------------

        I also had need for a more convenient (perl) validate function. So in 
CGI::FormBuilder::Field, in function validate() I added :

        Just after '# Check our hash to see if it's a special pattern' I 
changed the line to :
-----------------------------------------------------------------------
     if (ref $pattern eq 'ARRAY') {
        $self->{array} = 1;
        $VALIDATE{$_} && (($_) = autodata($VALIDATE{$_})) for @{$pattern};
     } else {
        $self->{array} = 0;
        ($pattern) = autodata($VALIDATE{$pattern}) if $VALIDATE{$pattern};
     }
-----------------------------------------------------------------------

        Next, after '} elsif (ref $pattern eq 'ARRAY') {' I changed the block 
to :
-----------------------------------------------------------------------
            my $cnt = 0;
            foreach my $elem (@{$pattern}) {
                if ($elem =~ m,^m(\S)(.*)\1$, || $elem =~ m,^(/)(.*)\1$,) {
                    # it be a regexp, handle / escaping
                    (my $tpat = $2) =~ s#\\/#/#g;
                    $tpat =~ s#/#\\/#g;
                    debug 2, "$field: does '$value' =~ /$tpat/ ?";
                    unless ($value =~ /$tpat/) {
                        $thisfail = ++$bad;
                        push @failed, $cnt;
                    }
                } else {                
                    # must be w/i this set of values
                    debug 2, "$field: is '$value' eq $elem ?";
                    unless ($value eq $elem) {
                        $thisfail = ++$bad;
                        push @failed, $cnt;
                    }
                }
                $cnt++;
            }
-----------------------------------------------------------------------

        Finally, after '# If not $atleastone and they asked for validation, 
then we
     # know that we have an error since this means no values' I changed 
the block to :

-----------------------------------------------------------------------
     if ($bad || (! $atleastone && $self->required)) {
         debug 1, "$field: validation FAILED";
         $self->{invalid} = $bad || 1;
         $self->{failed} = \@failed;
         $self->{missing} = $atleastone;
         return;
     } else {
         debug 1, "$field: validation passed";
         delete $self->{invalid};    # in case of previous run
         delete $self->{failed};
         delete $self->{missing};    # ditto
         return 1;
     }
-----------------------------------------------------------------------

        The purpose of all of this is to have a good interpretation of the 
commas in regexps, and also to provide the number of the message 
corresponding to the pattern that failed.

        In my TT template I have this :

-----------------------------------------------------------------------
[% FOREACH field IN formbuilder.fields %]
   [% l(field.label) %] [% IF field.required %] <strong>*</strong> [% END %]
   [% IF field.invalid %]
     <span class="error small">
     [% IF field.array %]
       [% FOREACH msgnb IN field.failed %]
         <br>[% l(field.message.$msgnb) %]
       [% END %]
     [% ELSE %]
       [% l(field.message) %]
     [% END %]
     </span>
   [% END %]
   <br>
   [% field.field %]
   <br><br>
[% END %]
-----------------------------------------------------------------------

        where l() is a macro for localization.

        I hope not to be too confusing, I can provide a real patch if needed (I 
am lazy today ;-) )

        HTH,

        Regards,
-- 
--      \^/                                            --
--    -/ O \---------------------------------------    --
--   | |/ \|      Alexandre (Midnite) Jousset      |   --
--    -|___|---------------------------------------    --
_______________________________________________
FBusers mailing list
FBusers@xxxxxxxxxxxxxxx
http://www.formbuilder.org/mailman/listinfo/fbusers



<Prev in Thread] Current Thread [Next in Thread>