summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2008-10-24 10:18:36 +0200
committerYorhel <git@yorhel.nl>2008-10-24 10:18:36 +0200
commit33087850cdda3437814be7084114b3601e447604 (patch)
tree7cd4a5392d01afb3e4429a86e5893efeeb4b6f38
parent94e6c9968657a2bfb7f8bb7b754117e1a03c6a40 (diff)
formValidate: Actually made the required rule the default, documented the func rule, more advanced example
Just found out I could modify the value without having to change anything in the framework code, which is pretty nice.
-rw-r--r--lib/VNDB/Handler/Forms.pm6
-rw-r--r--lib/YAWF/Misc.pm6
2 files changed, 10 insertions, 2 deletions
diff --git a/lib/VNDB/Handler/Forms.pm b/lib/VNDB/Handler/Forms.pm
index b75148b..b39cc44 100644
--- a/lib/VNDB/Handler/Forms.pm
+++ b/lib/VNDB/Handler/Forms.pm
@@ -17,10 +17,12 @@ qr/formtest/, sub {
my $self = shift;
my @rules;
- my $rules = qq{ (
+ my $rules = q{ (
{ name => 'string1', required => 1, maxlength => 20, minlength => 10, enum => [qw|50 hundred only-valid-value|] },
{ name => 'string2', required => 0, regex => [ qr/default/, 'fail message' ], default => 'this is a default (and matches)' },
- { name => 'string3', required => 1, func => [ sub { length shift > 5 }, 'another fail message' ] },
+ { name => 'string3', required => 1, func => [ sub {
+ if(length $_[0] > 5) { $_[0] = uc($_[0]); return $_[0] } else { return undef }
+ }, 'another fail message' ] },
{ name => 'string4', required => 1, template => 'int' },
)};
eval '@rules = '.$rules;
diff --git a/lib/YAWF/Misc.pm b/lib/YAWF/Misc.pm
index 89a0e04..07bffef 100644
--- a/lib/YAWF/Misc.pm
+++ b/lib/YAWF/Misc.pm
@@ -37,6 +37,11 @@ our %templates = (
# func => [ sub { external subroutine }, "message for the user" ]
# }
#
+# The subroutine passed with the func rule will receive a form value as it's
+# first argument and must return either any false value if the field doesn't
+# validate or any true value otherwise. The function is allowed to modify it's
+# first argument to change the value of the field.
+#
# Returns a hash with form names as keys and their value as argument, and
# a special key called '_err' in case there were errors. The value of this
# hash item is an array of failed test cases, each represented as an array
@@ -54,6 +59,7 @@ sub formValidate {
my %ret;
for my $f (@fields) {
+ $f->{required}++ if not exists $f->{required};
my @values = $f->{multi} ? $self->reqParam($f->{name}) : ( scalar $self->reqParam($f->{name}) );
$values[0] = '' if !@values;
for (@values) {