| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | # | ||
| 4 | # | ||
| 5 | # FormMagick (c) 2000-2001 Kirrily Robert <skud@cpan.org> | ||
| 6 | # Copyright (c) 2000-2002 Mitel Networks Corporation | ||
| 7 | # This software is distributed under the same licenses as Perl itself; | ||
| 8 | # see the file COPYING for details. | ||
| 9 | # | ||
| 10 | # $Id: Validator.pm,v 1.48 2003/02/05 17:18:38 anoncvs_gtkglext Exp $ | ||
| 11 | # | ||
| 12 | |||
| 13 | package CGI::FormMagick::Validator; | ||
| 14 | use strict; | ||
| 15 | |||
| 16 | require Exporter; | ||
| 17 | our @ISA = qw( Exporter ); | ||
| 18 | our @EXPORT = qw( | ||
| 19 | do_validation_routine | ||
| 20 | parse_validation_routine | ||
| 21 | call_user_validation | ||
| 22 | call_fm_validation | ||
| 23 | |||
| 24 | get_validation_attribute | ||
| 25 | validate_field | ||
| 26 | validate_page | ||
| 27 | validate_all | ||
| 28 | list_error_messages | ||
| 29 | errors | ||
| 30 | |||
| 31 | nonblank | ||
| 32 | integer | ||
| 33 | number | ||
| 34 | word | ||
| 35 | date | ||
| 36 | |||
| 37 | maxlength | ||
| 38 | minlength | ||
| 39 | exactlength | ||
| 40 | lengthrange | ||
| 41 | |||
| 42 | url | ||
| 43 | username | ||
| 44 | password | ||
| 45 | domain_name | ||
| 46 | ip_number | ||
| 47 | email_simple | ||
| 48 | mac_address | ||
| 49 | |||
| 50 | US_state | ||
| 51 | US_zipcode | ||
| 52 | iso_country_code | ||
| 53 | |||
| 54 | credit_card_number | ||
| 55 | credit_card_expiry | ||
| 56 | ); | ||
| 57 | |||
| 58 | |||
| 59 | use CGI::FormMagick::Validator::Basic; | ||
| 60 | use CGI::FormMagick::Validator::Length; | ||
| 61 | use CGI::FormMagick::Validator::Network; | ||
| 62 | use CGI::FormMagick::Validator::Geography; | ||
| 63 | use CGI::FormMagick::Validator::Business; | ||
| 64 | |||
| 65 | use CGI::FormMagick::Sub; | ||
| 66 | |||
| 67 | =pod | ||
| 68 | |||
| 69 | =head1 NAME | ||
| 70 | |||
| 71 | CGI::FormMagick::Validator - validate data from FormMagick forms | ||
| 72 | |||
| 73 | =head1 SYNOPSIS | ||
| 74 | |||
| 75 | use CGI::FormMagick; | ||
| 76 | |||
| 77 | =head1 DESCRIPTION | ||
| 78 | |||
| 79 | This module provides some common validation routines. Validation | ||
| 80 | routines return the string "OK" if they succeed, or a descriptive | ||
| 81 | message if they fail. | ||
| 82 | |||
| 83 | =for testing | ||
| 84 | BEGIN: { | ||
| 85 | use CGI::FormMagick; | ||
| 86 | use CGI::FormMagick::Validator; | ||
| 87 | use CGI; | ||
| 88 | use vars qw($fm); | ||
| 89 | } | ||
| 90 | $fm = CGI::FormMagick->new(type => 'file', source => 't/simple.xml'); | ||
| 91 | $fm->parse_xml(); # suck in structure without display()ing | ||
| 92 | |||
| 93 | =head2 Validation routines provided: | ||
| 94 | |||
| 95 | See the following for information on categories of validation we've | ||
| 96 | provided: | ||
| 97 | |||
| 98 | =over 4 | ||
| 99 | |||
| 100 | =item * | ||
| 101 | |||
| 102 | L<CGI::FormMagick::Validator::Basic> | ||
| 103 | |||
| 104 | =item * | ||
| 105 | |||
| 106 | L<CGI::FormMagick::Validator::Length> | ||
| 107 | |||
| 108 | =item * | ||
| 109 | |||
| 110 | L<CGI::FormMagick::Validator::Network> | ||
| 111 | |||
| 112 | =item * | ||
| 113 | |||
| 114 | L<CGI::FormMagick::Validator::Geography> | ||
| 115 | |||
| 116 | =item * | ||
| 117 | |||
| 118 | L<CGI::FormMagick::Validator::Business> | ||
| 119 | |||
| 120 | =back | ||
| 121 | |||
| 122 | |||
| 123 | =head2 Using more than one validation routine per field | ||
| 124 | |||
| 125 | You can use multiple validation routines like this: | ||
| 126 | |||
| 127 | value="foo" validation="my_routine, my_other_routine" | ||
| 128 | |||
| 129 | However, there are some requirements on formatting to make sure that | ||
| 130 | FormMagick can parse what you've given it. | ||
| 131 | |||
| 132 | =over 4 | ||
| 133 | |||
| 134 | =item * | ||
| 135 | |||
| 136 | Parens are optional on subroutines with no args. C<my_routine> is | ||
| 137 | equivalent to C<my_routine()>. | ||
| 138 | |||
| 139 | =item * | ||
| 140 | |||
| 141 | You B<MUST> put a comma then a space between routine names, eg | ||
| 142 | C<my_routine, my_other_routine> B<NOT> C<my_routine,my_other_routine>. | ||
| 143 | |||
| 144 | =item * | ||
| 145 | |||
| 146 | You B<MUST NOT> put a space between args to a routine, eg | ||
| 147 | C<my_routine(1,2,3)> B<NOT> C<my_routine(1, 2, 3)>. | ||
| 148 | |||
| 149 | =back | ||
| 150 | |||
| 151 | This will be fixed to be more flexible in a later release. | ||
| 152 | |||
| 153 | =head2 Making your own routines | ||
| 154 | |||
| 155 | FormMagick's validation routines may be overridden and others may be added on | ||
| 156 | a per-application basis. To do this, simply define a subroutine in your | ||
| 157 | CGI script that works in a similar way to the routines provided by | ||
| 158 | CGI::FormMagick::Validator and use its name in the validation attribute | ||
| 159 | in your XML. | ||
| 160 | |||
| 161 | The arguments passed to the validation routine are the value of the | ||
| 162 | field (to be validated) and any subsequent arguments given in the | ||
| 163 | validation attribute. For example: | ||
| 164 | |||
| 165 | value="foo" validation="my_routine" | ||
| 166 | ===> my_routine(foo) | ||
| 167 | |||
| 168 | value="foo" validation="my_routine(42)" | ||
| 169 | ===> my_routine(foo, 42) | ||
| 170 | |||
| 171 | The latter type of validation routine is useful for routines like | ||
| 172 | C<minlength()> and C<lengthrange()> which come with | ||
| 173 | CGI::FormMagick::Validator. | ||
| 174 | |||
| 175 | Here's an example routine that you might write: | ||
| 176 | |||
| 177 | sub my_grep { | ||
| 178 | my $data = shift; | ||
| 179 | my @list = @_; | ||
| 180 | if (grep /$data/, @list) { | ||
| 181 | return "OK" | ||
| 182 | } else { | ||
| 183 | return "That's not one of: @list" | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | =pod | ||
| 189 | |||
| 190 | =head1 SECURITY CONSIDERATIONS AND METHODS FOR MANUAL VALIDATION | ||
| 191 | |||
| 192 | If you use page post-event or pre-event routines which perform code | ||
| 193 | which is in any way based on user input, your application may be | ||
| 194 | susceptible to a security exploit. | ||
| 195 | |||
| 196 | The exploit can occur as follows: | ||
| 197 | |||
| 198 | Imagine you have an application with three pages. Page 1 has fields A, | ||
| 199 | B and C. Page 2 has fields D, E and F. Page 3 has fields G, H and I. | ||
| 200 | |||
| 201 | The user fills in page 1 and the data FOR THAT PAGE is validated before | ||
| 202 | they're allowed to move on to page 2. When they fill in page 2, the | ||
| 203 | data FOR THAT PAGE is validated before they can move on. Ditto for page | ||
| 204 | 3. | ||
| 205 | |||
| 206 | If the user saves a copy of page 2 and edits it to contain an extra | ||
| 207 | field, "A", with an invalid value, then submits that page back to | ||
| 208 | FormMagick, the added field "A" will NOT be validated. | ||
| 209 | |||
| 210 | This is because FormMagick relies on the XML description of the page to | ||
| 211 | know what fields to validate. Only the current page's fields are | ||
| 212 | validated, until the very end when all the fields are revalidated one | ||
| 213 | last time before the form post-event is run. This means that we don't | ||
| 214 | suffer the load of validating everything every time, and it will work | ||
| 215 | fine for most applications. | ||
| 216 | |||
| 217 | However, if you need to run page post-event or pre-event routines that | ||
| 218 | rely on previous pages' data, you should validate it manually in your | ||
| 219 | post-event or pre-event routine. The following methods are used | ||
| 220 | internally by FormMagick for its validation, but may also be useful to | ||
| 221 | developers. | ||
| 222 | |||
| 223 | Note: this stuff may be buggy. Please let us know how you go with it. | ||
| 224 | |||
| 225 | =head2 $fm->validate_field($fieldname | $fieldref) | ||
| 226 | |||
| 227 | This routine allows you to validate a specific field by hand if you need | ||
| 228 | to. It returns an arrayref containing a list of error messages if | ||
| 229 | validation fails, or the string "OK" on success. | ||
| 230 | |||
| 231 | Examples of use: | ||
| 232 | |||
| 233 | This is how you'd probably call it from your script: | ||
| 234 | |||
| 235 | if ($fm->validate_field("credit_card_number") eq "OK")) { } | ||
| 236 | |||
| 237 | FormMagick uses references to a field object, internally: | ||
| 238 | |||
| 239 | if ($fm->validate_field($fieldref) eq "OK")) { } | ||
| 240 | |||
| 241 | (that's so that FormMagick can easily loop through the fields in a page; | ||
| 242 | you shouldn't need to do that) | ||
| 243 | |||
| 244 | If you want to do something with the error messages returned: | ||
| 245 | |||
| 246 | my $errors = $fm->validate_field($field); | ||
| 247 | if (ref $errors) { | ||
| 248 | foreach my $e (@$errors) { | ||
| 249 | do_something(); | ||
| 250 | } | ||
| 251 | } else { | ||
| 252 | # it's OK | ||
| 253 | } | ||
| 254 | |||
| 255 | =begin testing | ||
| 256 | local $fm->{cgi}; # we're going to mess with the CGI fields | ||
| 257 | |||
| 258 | my $field = { | ||
| 259 | validation => 'nonblank', | ||
| 260 | id => 'testfield', | ||
| 261 | label => 'Test Field', | ||
| 262 | }; | ||
| 263 | |||
| 264 | my $goodcgi = CGI->new( { testfield => 'testing' } ); | ||
| 265 | $fm->{cgi} = $goodcgi; | ||
| 266 | is($fm->validate_field($field), "OK", "Test a single field"); | ||
| 267 | |||
| 268 | my $badcgi = CGI->new( { testfield => '' } ); | ||
| 269 | $fm->{cgi} = $badcgi; | ||
| 270 | isnt($fm->validate_field($field), "OK", "Test a single field"); | ||
| 271 | is(ref($fm->validate_field($field)), ARRAY, "return an arrayref"); | ||
| 272 | |||
| 273 | TODO: { | ||
| 274 | local $TODO = "Make validate_field accept a fieldname instead of a fieldref"; | ||
| 275 | is($fm->validate_field("firstname"), "OK", "validate_field accepts field names"); | ||
| 276 | } | ||
| 277 | |||
| 278 | =end testing | ||
| 279 | |||
| 280 | =cut | ||
| 281 | |||
| 282 | sub validate_field { | ||
| 283 | my ($self, $field) = @_; | ||
| 284 | |||
| 285 | if (not ref $field) { | ||
| 286 | return undef; # TODO: make this take fieldnames, not just fieldrefs. | ||
| 287 | } | ||
| 288 | |||
| 289 | my $validation = $self->get_validation_attribute($field); | ||
| 290 | my $fieldname = $field->{id}; | ||
| 291 | my $fieldlabel = $field->{label} || ""; | ||
| 292 | my $fielddata = $self->{cgi}->param($fieldname); | ||
| 293 | |||
| 294 | $self->debug_msg('Validating field ' . (defined $fieldname ? $fieldname : '')); | ||
| 295 | |||
| 296 | # just skip everything else if there's no validation to do. | ||
| 297 | return "OK" unless $validation; | ||
| 298 | |||
| 299 | my @results; | ||
| 300 | # XXX argh! this split statement requires that we write validators like | ||
| 301 | # "lengthrange(4, 10), word" like "lengthrange(4,10), word" in order to | ||
| 302 | # work. Eeek. That's not how this should work. But it was even | ||
| 303 | # more broken before (I changed a * to a +). | ||
| 304 | # OTOH, I'm not sure it's fixed now. --srl | ||
| 305 | |||
| 306 | my @validation_routines = split( /,\s+/, $validation); | ||
| 307 | # $self->debug_msg("Going to perform these validation routines: @validation_routines"); | ||
| 308 | |||
| 309 | foreach my $v (@validation_routines) { | ||
| 310 | my ($validator, $arg) = $self->parse_validation_routine($v); | ||
| 311 | my $result = $self->do_validation_routine($validator, $fielddata, $arg); | ||
| 312 | |||
| 313 | push (@results, $result) if $result ne "OK"; | ||
| 314 | |||
| 315 | # for multiple errors, put semicolons between the errors before | ||
| 316 | # shoving them in a hash to return. | ||
| 317 | |||
| 318 | } | ||
| 319 | |||
| 320 | if (@results) { | ||
| 321 | return \@results; | ||
| 322 | } else { | ||
| 323 | return "OK"; | ||
| 324 | } | ||
| 325 | } | ||
| 326 | |||
| 327 | =head2 get_validation_attribute($field) | ||
| 328 | |||
| 329 | A tiny little routine which, given a field hashref (as seen in | ||
| 330 | validate_field() will give you the value of the validation attribute | ||
| 331 | from that field. | ||
| 332 | |||
| 333 | This was split out to make it easy to have a subclass add validation | ||
| 334 | routines by overriding this function. | ||
| 335 | |||
| 336 | =begin testing | ||
| 337 | |||
| 338 | can_ok($fm, "get_validation_attribute"); | ||
| 339 | |||
| 340 | my $field = { | ||
| 341 | validation => 'nonblank', | ||
| 342 | id => 'testfield', | ||
| 343 | label => 'Test Field', | ||
| 344 | }; | ||
| 345 | |||
| 346 | is($fm->get_validation_attribute($field), "nonblank", "get_validation_attribute"); | ||
| 347 | |||
| 348 | =end testing | ||
| 349 | |||
| 350 | =cut | ||
| 351 | |||
| 352 | sub get_validation_attribute { | ||
| 353 | my ($fm, $field) = @_; | ||
| 354 | return $field->{validation}; | ||
| 355 | } | ||
| 356 | |||
| 357 | =pod | ||
| 358 | |||
| 359 | =head2 $fm->validate_page($number | $name) | ||
| 360 | |||
| 361 | This routine allows you to validate a single page worth of fields. It | ||
| 362 | can accept either a page number (counting from zero) or a page name. | ||
| 363 | |||
| 364 | This routine returns a hash of errors, with the keys being the names of | ||
| 365 | fields which have errors and the values being the error messages. An | ||
| 366 | empty hash means no errors. | ||
| 367 | |||
| 368 | The routine will return undef if it can't figure out what page you want. | ||
| 369 | |||
| 370 | Examples: | ||
| 371 | |||
| 372 | my %errors = $fm->validate_page(3); | ||
| 373 | my %errors = $fm->validate_page("CreditCardDetails"); | ||
| 374 | if (%errors) { ... } | ||
| 375 | |||
| 376 | =begin testing | ||
| 377 | |||
| 378 | local $fm->{cgi} = CGI->new( { | ||
| 379 | firstname => 'testing', # this is known-good | ||
| 380 | lastname => '', # bad. should be nonblank. | ||
| 381 | long => "abc", # bad. should be long. | ||
| 382 | short => "abcdefghijk", # bad. should be short. | ||
| 383 | } ); | ||
| 384 | |||
| 385 | my @pagenames = ("Personal", "More", "More again"); | ||
| 386 | foreach (0..2) { | ||
| 387 | my %errors = $fm->validate_page($_); | ||
| 388 | is(scalar keys %errors, $_, "Test page '$_' with $_ known errors"); | ||
| 389 | my %name_errors = $fm->validate_page($pagenames[$_]); | ||
| 390 | is(scalar keys %name_errors, $_, "Test erroring page '$pagenames[$_]'"); | ||
| 391 | } | ||
| 392 | |||
| 393 | $fm->{cgi} = CGI->new( { | ||
| 394 | firstname => 'willy', | ||
| 395 | lastname => 'wonka', | ||
| 396 | long => "abcdefg", | ||
| 397 | short => "abc", | ||
| 398 | } ); | ||
| 399 | |||
| 400 | foreach (@pagenames) { | ||
| 401 | my %errors = $fm->validate_page($_); | ||
| 402 | is(scalar keys %errors, 0, "Test page '$_' without errors"); | ||
| 403 | } | ||
| 404 | |||
| 405 | ok(!defined $fm->validate_page("abcde"), "Validate page returns undef for a non-page"); | ||
| 406 | ok(!defined $fm->validate_page(), "Validate page returns undef no args"); | ||
| 407 | |||
| 408 | =end testing | ||
| 409 | |||
| 410 | =cut | ||
| 411 | |||
| 412 | sub validate_page { | ||
| 413 | my ($self, $param) = @_; | ||
| 414 | |||
| 415 | return undef unless defined $param; | ||
| 416 | |||
| 417 | my $page_index; # what page number is this? | ||
| 418 | |||
| 419 | my $is_integer_like; | ||
| 420 | { | ||
| 421 | local $^W = 0; | ||
| 422 | $is_integer_like = int($param) eq $param; | ||
| 423 | } | ||
| 424 | if ($is_integer_like) { | ||
| 425 | $page_index = $param; | ||
| 426 | } else { | ||
| 427 | $page_index = $self->get_page_by_name($param); | ||
| 428 | } | ||
| 429 | return undef unless defined $page_index; | ||
| 430 | |||
| 431 | $self->debug_msg("Validating page $page_index."); | ||
| 432 | |||
| 433 | my %errors; | ||
| 434 | |||
| 435 | my $this_page = $self->form->{pages}->[$page_index]; | ||
| 436 | |||
| 437 | foreach my $field (@{$this_page->{fields}}) { | ||
| 438 | my $result = $self->validate_field($field); | ||
| 439 | unless ($result eq "OK") { | ||
| 440 | $errors{$field->{label}} = $result; | ||
| 441 | } | ||
| 442 | } | ||
| 443 | |||
| 444 | my $howmany = (keys %errors); | ||
| 445 | $self->debug_msg("Done validating page $page_index. Found $howmany errors."); | ||
| 446 | |||
| 447 | $self->{errors} = \%errors; | ||
| 448 | return %errors; | ||
| 449 | } | ||
| 450 | |||
| 451 | =pod | ||
| 452 | |||
| 453 | =head2 $fm->validate_all() | ||
| 454 | |||
| 455 | This routine goes through all the pages that have been visited (using | ||
| 456 | FormMagick's built-in page stack to keep track of which these are) and | ||
| 457 | runs C<validate_page()> on each of them. | ||
| 458 | |||
| 459 | Returns a hash of all errors, and set $self->{errors} when done. | ||
| 460 | |||
| 461 | =begin testing | ||
| 462 | |||
| 463 | local $fm->{cgi} = CGI->new( { | ||
| 464 | firstname => 'testing', # this is known-good | ||
| 465 | lastname => '', # bad. should be nonblank. | ||
| 466 | long => "abc", # bad. should be long. | ||
| 467 | short => "abcdefghijk", # bad. should be short. | ||
| 468 | } ); | ||
| 469 | |||
| 470 | local $fm->{page_stack} = "0,1"; | ||
| 471 | local $fm->{page_number} = 2; | ||
| 472 | |||
| 473 | my %errors = $fm->validate_all(); | ||
| 474 | is(scalar keys %errors, 3, "Test all pages at once."); | ||
| 475 | |||
| 476 | =end testing | ||
| 477 | |||
| 478 | =cut | ||
| 479 | |||
| 480 | sub validate_all { | ||
| 481 | my ($self) = @_; | ||
| 482 | |||
| 483 | my %errors; | ||
| 484 | |||
| 485 | $self->debug_msg("Validating all form input."); | ||
| 486 | |||
| 487 | # Walk through all the pages on the stack and make sure | ||
| 488 | # the data for their fields is still valid | ||
| 489 | foreach my $pagenum ( (split(/,/, $self->{page_stack})), $self->{page_number} ) { | ||
| 490 | # add the errors from this page to the errors from any other pages | ||
| 491 | %errors = ( %errors, $self->validate_page($pagenum) ); | ||
| 492 | } | ||
| 493 | |||
| 494 | $self->{errors} = \%errors; | ||
| 495 | return %errors; | ||
| 496 | } | ||
| 497 | |||
| 498 | |||
| 499 | =pod | ||
| 500 | |||
| 501 | =head1 DEVELOPER METHODS | ||
| 502 | |||
| 503 | The following methods are probably not of interest to anyone except | ||
| 504 | developers of FormMagick | ||
| 505 | |||
| 506 | |||
| 507 | =head2 parse_validation_routine ($validation_routine_name) | ||
| 508 | |||
| 509 | parse the name of a validation routine into its name and its parameters. | ||
| 510 | returns a 2-element list, $validator and $arg. | ||
| 511 | |||
| 512 | =for testing | ||
| 513 | my @rv = $fm->parse_validation_routine("foo(1,2)"); | ||
| 514 | is($rv[0], "foo", "Pick up validation routine name"); | ||
| 515 | is($rv[1], "1,2", "Pick up validation routine args"); | ||
| 516 | |||
| 517 | =cut | ||
| 518 | |||
| 519 | sub parse_validation_routine { | ||
| 520 | my ($self, $validation_routine_name) = @_; | ||
| 521 | |||
| 522 | my ($validator, $arg) = ($validation_routine_name =~ | ||
| 523 | m/ | ||
| 524 | ^ # start of string | ||
| 525 | (\w+) # a word (--> $validator) | ||
| 526 | (?: # non-capturing (to group the (.*)) | ||
| 527 | \( # literal paren | ||
| 528 | (.*) # whatever's inside the paren (--> $arg) | ||
| 529 | \) # literal close paren | ||
| 530 | )? # (.*) is optional (zero or one of them) | ||
| 531 | $ # end of string | ||
| 532 | /x ); | ||
| 533 | |||
| 534 | return ($validator, $arg); | ||
| 535 | } | ||
| 536 | |||
| 537 | =pod | ||
| 538 | |||
| 539 | =head2 do_validation_routine ($self, $validator, $fielddata, $args) | ||
| 540 | |||
| 541 | Runs validation functions with arguments. Looks first for a user | ||
| 542 | routine, then for a builtin, then if it can't find either it just kinda | ||
| 543 | shrugs and says "OK". | ||
| 544 | |||
| 545 | Returns "OK" if validation is successful, or the error message returned | ||
| 546 | by the validation routine otherwise. | ||
| 547 | |||
| 548 | =begin testing | ||
| 549 | |||
| 550 | sub user1 { | ||
| 551 | return "OK"; | ||
| 552 | } | ||
| 553 | |||
| 554 | is($fm->do_validation_routine("nonblank", "abc"), "OK", | ||
| 555 | "Find builtin validation routine"); | ||
| 556 | is($fm->do_validation_routine("user1", "abc"), "OK", | ||
| 557 | "Find user validation routine"); | ||
| 558 | { | ||
| 559 | local $^W = 0; | ||
| 560 | is($fm->do_validation_routine("nosuchthing", "abc"), "OK", | ||
| 561 | "Default to OK if you can't find a validation routine"); | ||
| 562 | } | ||
| 563 | |||
| 564 | =end testing | ||
| 565 | |||
| 566 | =cut | ||
| 567 | |||
| 568 | sub do_validation_routine { | ||
| 569 | my ($self, $validator, $fielddata, $args) = @_; | ||
| 570 | $fielddata ||= ""; | ||
| 571 | my $result; | ||
| 572 | |||
| 573 | my $cp = $self->{calling_package}; | ||
| 574 | |||
| 575 | # TODO: this could use some documentation. | ||
| 576 | # TODO: It could also use CGI::FormMagick::Sub::exists() directly? | ||
| 577 | $result = | ||
| 578 | $self->call_user_validation($validator, $fielddata, $args) || | ||
| 579 | $self->call_fm_validation ($validator, $fielddata, $args); | ||
| 580 | |||
| 581 | if (!$result) { | ||
| 582 | warn "Couldn't find validator $validator\n" if $^W; | ||
| 583 | $result = "OK"; # if we can't figure it out, just go with OK | ||
| 584 | } | ||
| 585 | |||
| 586 | $self->debug_msg("Validation result is $result"); | ||
| 587 | return $result; | ||
| 588 | } | ||
| 589 | |||
| 590 | =head2 $fm->call_user_validation_routine($routine, $data, $args) | ||
| 591 | |||
| 592 | Calls the user's validation routine, in the calling package (usually | ||
| 593 | main). | ||
| 594 | |||
| 595 | =begin testing | ||
| 596 | sub usertest_ok { | ||
| 597 | shift; | ||
| 598 | return "OK"; | ||
| 599 | } | ||
| 600 | |||
| 601 | sub usertest_data { | ||
| 602 | shift; | ||
| 603 | return shift; | ||
| 604 | } | ||
| 605 | |||
| 606 | sub usertest_arg1 { | ||
| 607 | shift; | ||
| 608 | return $_[1]; | ||
| 609 | } | ||
| 610 | |||
| 611 | sub usertest_arg2 { | ||
| 612 | shift; | ||
| 613 | return $_[2]; | ||
| 614 | } | ||
| 615 | |||
| 616 | is($fm->call_user_validation("usertest_ok", "", ""), "OK", | ||
| 617 | "Call a simple user validation routine"); | ||
| 618 | is($fm->call_user_validation("usertest_data", "FOO", ""), "FOO", | ||
| 619 | "Call a user validation routine with data"); | ||
| 620 | is($fm->call_user_validation("usertest_arg1", "", "bar"), "bar", | ||
| 621 | "Call a user validation routine with one arg"); | ||
| 622 | is($fm->call_user_validation("usertest_arg2", "", "bar,baz"), "baz", | ||
| 623 | "Call a user validation routine with two args"); | ||
| 624 | |||
| 625 | =end testing | ||
| 626 | |||
| 627 | =cut | ||
| 628 | |||
| 629 | sub call_user_validation { | ||
| 630 | my ($self, $validator, $data, $args) = @_; | ||
| 631 | |||
| 632 | my %sub = ( | ||
| 633 | package => $self->{calling_package}, | ||
| 634 | sub => $validator, | ||
| 635 | args => [ $self, $data ], | ||
| 636 | comma_delimited_args => $args, | ||
| 637 | ); | ||
| 638 | |||
| 639 | CGI::FormMagick::Sub::exists(%sub) or return undef; | ||
| 640 | return CGI::FormMagick::Sub::call(%sub); | ||
| 641 | } | ||
| 642 | |||
| 643 | =head2 $fm->call_fm_validation($routine, $data, $args) | ||
| 644 | |||
| 645 | Calls a builtin validation routine in CGI::FormMagick::Validator. | ||
| 646 | |||
| 647 | =begin testing | ||
| 648 | |||
| 649 | is($fm->call_fm_validation("nonblank", "abc", ""), "OK", | ||
| 650 | "Call a simple builtin validation routine"); | ||
| 651 | isnt($fm->call_fm_validation("nonblank", "", ""), "OK", | ||
| 652 | "Call a simple builtin validation routine"); | ||
| 653 | is($fm->call_fm_validation("minlength", "abc", "2"), "OK", | ||
| 654 | "Call a builtin validation routine with args"); | ||
| 655 | is($fm->call_fm_validation("lengthrange", "abc", "1,3"), "OK", | ||
| 656 | "Call a builtin validation routine with multiple args"); | ||
| 657 | |||
| 658 | =end testing | ||
| 659 | |||
| 660 | =cut | ||
| 661 | |||
| 662 | |||
| 663 | sub call_fm_validation { | ||
| 664 | my ($self, $validator, $data, $args) = @_; | ||
| 665 | my %sub = ( | ||
| 666 | package => 'CGI::FormMagick::Validator', | ||
| 667 | sub => $validator, | ||
| 668 | args => [ $self, $data ], | ||
| 669 | comma_delimited_args => $args, | ||
| 670 | ); | ||
| 671 | |||
| 672 | CGI::FormMagick::Sub::exists(%sub) or return undef; | ||
| 673 | |||
| 674 | return CGI::FormMagick::Sub::call(%sub); | ||
| 675 | } | ||
| 676 | |||
| 677 | |||
| 678 | |||
| 679 | =pod | ||
| 680 | |||
| 681 | =head2 list_error_messages() | ||
| 682 | |||
| 683 | prints a list of error messages caused by validation failures | ||
| 684 | |||
| 685 | =cut | ||
| 686 | |||
| 687 | sub list_error_messages { | ||
| 688 | my $self = shift; | ||
| 689 | print qq(<div class="error">\n); | ||
| 690 | print qq(<h3>Errors</h3>\n); | ||
| 691 | print "<ul>"; | ||
| 692 | |||
| 693 | foreach my $field (keys %{$self->{errors}}) { | ||
| 694 | print "<li>$field: $self->{errors}->{$field}\n"; | ||
| 695 | } | ||
| 696 | print "</ul></div>\n"; | ||
| 697 | } | ||
| 698 | |||
| 699 | |||
| 700 | sub errors { | ||
| 701 | my $self = shift; | ||
| 702 | if ($self->{errors}) { | ||
| 703 | return %{$self->{errors}}; | ||
| 704 | } else { | ||
| 705 | return (); | ||
| 706 | } | ||
| 707 | } | ||
| 708 | |||
| 709 | |||
| 710 | =pod | ||
| 711 | |||
| 712 | =head1 SEE ALSO | ||
| 713 | |||
| 714 | The main perldoc for CGI::FormMagick | ||
| 715 | |||
| 716 | =head1 AUTHOR | ||
| 717 | |||
| 718 | Kirrily "Skud" Robert <skud@infotrope.net> | ||
| 719 | |||
| 720 | More information about FormMagick may be found at | ||
| 721 | http://sourceforge.net/projects/formmagick/ | ||
| 722 | |||
| 723 | =cut | ||
| 724 | |||
| 725 | return 1; |