14 Mar 09:31
Re: [FormBuilder] validate + commas in FB source files
From: Alexandre Jousset <mid <at> gtmp.org>
Subject: Re: [FormBuilder] validate + commas in FB source files
Newsgroups: gmane.comp.lang.perl.modules.formbuilder
Date: 2007-03-14 08:31:38 GMT
Subject: Re: [FormBuilder] validate + commas in FB source files
Newsgroups: gmane.comp.lang.perl.modules.formbuilder
Date: 2007-03-14 08:31:38 GMT
Hello Nate, Nate Wiger wrote: > Looks like a good addition. > >> I hope not to be too confusing, I can provide a real patch if >> needed (I >> am lazy today) > > Please do. Provide a diff -u and also a couple tests, and I'll apply > it to the tree. Ok, here it is, in attachment. With it, I can use this kind of YAML config file (I use Catalyst): File '.../MyApp/root/forms/user/create.fb': ----------------------------------------------------------- name: user_create method: post fields: login: label: Login type: text size: 40 validate: /^.{4\,40}$/, /^\w*$/ required: 1 message: Between 4 and 40 characters, Only alphanumeric characters disp_name: label: Real name (public) type: text size: 50 validate: /^.{0\,50}$/, LNAME message: Max 50 characters, Forbidden character(s) mail: label: Real Email type: text size: 80 validate: EMAIL required: 1 message: Invalid Email disp_mail: label: Displayed Email (public) type: text size: 80 validate: /^.{0\,80}$/ message: Max 80 characters home_page: label: URL type: text size: 100 validate: /^.{0\,100}$/ message: Invalid URL pass: label: Password type: password size: 40 validate: /^.{4\,40}$/ required: 1 message: Between 4 and 40 characters pass2: label: Password confirmation type: password size: 40 validate: /^.{4\,40}$/ required: 1 message: Between 4 and 40 characters submit: Create reset: Reset ----------------------------------------------------------- Notice in "validate:" items the values separated by a comma. Also, the corresponding "messages:" are also separated by a comma. Also notice the '\,' in the regexps, for which in the patch I remove the leading '\'. I won't repeat the template I use to render this form as I provided it in my previous message. The only thing to know is that a new attribute, $fb->{array} indicates an array instead of a single value. I'm looking for other predefined regexps, like URLs. I can build one myself but if I find one on the web / MLs, I'll also patch again the file Field.pm to include it. If you (or anyone) find a good one, please tell us. I'm also looking for a good way to translate the 'submit' and 'reset' buttons. Any clue ? Tell me if you need anything else. Unfortunately I've never built any Perl test suite, so I can't, for the moment, give you any. Sorry for this
Regards, -- -- \^/ -- -- -/ O \--------------------------------------- -- -- | |/ \| Alexandre (Midnite) Jousset | -- -- -|___|--------------------------------------- --
--- /root/.cpan/build/CGI-FormBuilder-3.0501/lib/CGI/FormBuilder/Field.pm 2007-03-02
19:13:14.000000000 +0100
+++ /usr/local/share/perl/5.8.8/CGI/FormBuilder/Field.pm 2007-03-13 12:31:21.000000000 +0100
@@ -620,7 +620,7 @@
elsif (ref $pattern eq 'ARRAY') {
# Must be w/i this set of values
# Can you figure out how this piece of Perl works? No, seriously, I forgot.
- $jsfunc .= qq[${in}if ($notnull ($jsfield != ']
+ $jsfunc .= qq[${in}if ($notnull ($jsfield != \']
. join("' && $jsfield != '", @{$pattern}) . "')) {\n";
}
elsif (ref $pattern eq 'CODE' || $pattern eq 'VALUE' || ($self->required && ! $pattern)) {
@@ -676,7 +676,13 @@
debug 1, "$self->{name}: called \$field->validate(@_) for field '$field'";
# Check our hash to see if it's a special pattern
- ($pattern) = autodata($VALIDATE{$pattern}) if $VALIDATE{$pattern};
+ if (ref $pattern eq 'ARRAY') {
+ $self->{array} = 1;
+ $VALIDATE{$_} && (($_) = autodata($VALIDATE{$_})) for @{$pattern};
+ } else {
+ $self->{array} = 0;
+ ($pattern) = autodata($VALIDATE{$pattern}) if $VALIDATE{$pattern};
+ }
# Hashref is a grouping per-language
if (ref $pattern eq 'HASH') {
@@ -685,6 +691,7 @@
# Counter for fail or success
my $bad = 0;
+ my @failed;
# Loop thru, and if something isn't valid, we tag it
my $atleastone = 0;
@@ -713,11 +720,27 @@
$thisfail = ++$bad;
}
} elsif (ref $pattern eq 'ARRAY') {
- # must be w/i this set of values
- debug 2, "$field: is '$value' in (@{$pattern}) ?";
- unless (ismember($value, @{$pattern})) {
- $thisfail = ++$bad;
- }
+ 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++;
+ }
} elsif (ref $pattern eq 'CODE') {
# eval that mofo, which gives them $form
my $extra = $form->{c} || $form;
@@ -769,11 +792,13 @@
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;
}
_______________________________________________ FBusers mailing list FBusers <at> formbuilder.org http://www.formbuilder.org/mailman/listinfo/fbusers
)
>
> Please do. Provide a diff -u and also a couple tests, and I'll apply
> it to the tree.
Ok, here it is, in attachment.
With it, I can use this kind of YAML config file (I use Catalyst):
File '.../MyApp/root/forms/user/create.fb':
-----------------------------------------------------------
name: user_create
method: post
fields:
login:
label: Login
type: text
size: 40
validate: /^.{4\,40}$/, /^\w*$/
required: 1
message: Between 4 and 40 characters, Only alphanumeric characters
disp_name:
label: Real name (public)
type: text
size: 50
validate: /^.{0\,50}$/, LNAME
message: Max 50 characters, Forbidden character(s)
mail:
label: Real Email
type: text
size: 80
validate: EMAIL
required: 1
message: Invalid Email
disp_mail:
label: Displayed Email (public)
type: text
size: 80
validate: /^.{0\,80}$/
message: Max 80 characters
home_page:
label: URL
type: text
size: 100
validate: /^.{0\,100}$/
message: Invalid URL
pass:
label: Password
type: password
size: 40
validate: /^.{4\,40}$/
required: 1
message: Between 4 and 40 characters
pass2:
label: Password confirmation
type: password
size: 40
validate: /^.{4\,40}$/
required: 1
message: Between 4 and 40 characters
submit: Create
reset: Reset
-----------------------------------------------------------
Notice in "validate:" items the values separated by a comma. Also, the
corresponding "messages:" are also separated by a comma.
Also notice the '\,' in the regexps, for which in the patch I remove
the leading '\'.
I won't repeat the template I use to render this form as I provided it
in my previous message. The only thing to know is that a new attribute,
$fb->{array} indicates an array instead of a single value.
I'm looking for other predefined regexps, like URLs. I can build one
myself but if I find one on the web / MLs, I'll also patch again the
file Field.pm to include it. If you (or anyone) find a good one, please
tell us.
I'm also looking for a good way to translate the 'submit' and 'reset'
buttons. Any clue ?
Tell me if you need anything else. Unfortunately I've never built any
Perl test suite, so I can't, for the moment, give you any. Sorry for
this
Regards,
--
-- \^/ --
-- -/ O \--------------------------------------- --
-- | |/ \| Alexandre (Midnite) Jousset | --
-- -|___|--------------------------------------- --
RSS Feed