1Data::FormValidator(3)User Contributed Perl DocumentationData::FormValidator(3)
2
3
4
6 Data::FormValidator - Validates user input (usually from an HTML form)
7 based on input profile.
8
10 use Data::FormValidator;
11
12 my $results = Data::FormValidator->check(\%input_hash, \%dfv_profile);
13
14 if ($results->has_invalid or $results->has_missing) {
15 # do something with $results->invalid, $results->missing
16 # or $results->msgs
17 }
18 else {
19 # do something with $results->valid
20 }
21
23 Data::FormValidator's main aim is to make input validation expressible
24 in a simple format.
25
26 Data::FormValidator lets you define profiles which declare the required
27 and optional fields and any constraints they might have.
28
29 The results are provided as an object, which makes it easy to handle
30 missing and invalid results, return error messages about which
31 constraints failed, or process the resulting valid data.
32
34 check()
35 my $results = Data::FormValidator->check(\%input_hash, \%dfv_profile);
36
37 "check" is the recommended method to use to validate forms. It returns
38 its results as a Data::FormValidator::Results object. A deprecated
39 method "validate" described below is also available, returning its
40 results as an array.
41
42 use Data::FormValidator;
43 my $results = Data::FormValidator->check(\%input_hash, \%dfv_profile);
44
45 Here, "check()" is used as a class method, and takes two required
46 parameters.
47
48 The first a reference to the data to be be validated. This can either
49 be a hash reference, or a CGI.pm-like object. In particular, the object
50 must have a param() method that works like the one in CGI.pm does.
51 CGI::Simple and Apache::Request objects are known to work in
52 particular. Note that if you use a hash reference, multiple values for
53 a single key should be presented as an array reference.
54
55 The second argument is a reference to the profile you are validating.
56
57 validate()
58 my( $valids, $missings, $invalids, $unknowns ) =
59 Data::FormValidator->validate( \%input_hash, \%dfv_profile);
60
61 "validate()" provides a deprecated alternative to "check()". It has the
62 same input syntax, but returns a four element array, described as
63 follows
64
65 valids
66 This is a hash reference to the valid fields which were submitted
67 in the data. The data may have been modified by the various filters
68 specified.
69
70 missings
71 This is a reference to an array which contains the name of the
72 missing fields. Those are the fields that the user forget to fill
73 or filled with spaces. These fields may comes from the required
74 list or the dependencies list.
75
76 invalids
77 This is a reference to an array which contains the name of the
78 fields which failed one or more of their constraint checks. If
79 there are no invalid fields, an empty arrayref will be returned.
80
81 Fields defined with multiple constraints will have an array ref
82 returned in the @invalids array instead of a string. The first
83 element in this array is the name of the field, and the remaining
84 fields are the names of the failed constraints.
85
86 unknowns
87 This is a list of fields which are unknown to the profile. Whether
88 or not this indicates an error in the user input is application
89 dependent.
90
91 new()
92 Using "new()" is only needed for advanced usage, including these cases:
93
94 o Loading more than one profile at a time. Then you can select the
95 profile you want by name later with "check()". Here's an example:
96
97 my $dfv = Data::FormValidator->new({
98 profile_1 => { # usual profile definition here },
99 profile_2 => { # another profile definition },
100 });
101
102 As illustrated, multiple profiles are defined through a hash ref
103 whose keys point to profile definitions.
104
105 You can also load several profiles from a file, by defining several
106 profiles as shown above in an external file. Then just pass in the
107 name of the file:
108
109 my $dfv = Data::FormValidator->new('/path/to/profiles.pl');
110
111 If the input profile is specified as a file name, the profiles will
112 be reread each time that the disk copy is modified.
113
114 Now when calling "check()", you just need to supply the profile
115 name:
116
117 my $results = $dfv->check(\%input_hash,'profile_1');
118
119 o Applying defaults to more than one input profile. There are some
120 parts of the validation profile that you might like to re-use for
121 many form validations.
122
123 To facilitate this, "new()" takes a second argument, a hash
124 reference. Here the usual input profile definitions can be made.
125 These will act as defaults for any subsequent calls to "check()" on
126 this object.
127
128 Currently the logic for this is very simple. Any definition of a
129 key in your validation profile will completely overwrite your
130 default value.
131
132 This means you can't define two keys for "constraint_regexp_map"
133 and expect they will always be there. This kind of feature may be
134 added in the future.
135
136 The exception here is definitions for your "msgs" key. You will
137 safely be able to define some defaults for the top level keys
138 within "msgs" and not have them clobbered just because "msgs" was
139 defined in a validation profile.
140
141 One way to use this feature is to create your own sub-class that
142 always provides your defaults to "new()".
143
144 Another option is to create your own wrapper routine which provides
145 these defaults to "new()". Here's an example of a routine you
146 might put in a CGI::Application super-class to make use of this
147 feature:
148
149 # Always use the built-in CGI object as the form data
150 # and provide some defaults to new constructor
151 sub check_form {
152 my $self = shift;
153 my $profile = shift
154 || die 'check_form: missing required profile';
155
156 require Data::FormValidator;
157 my $dfv = Data::FormValidator->new({},{
158 # your defaults here
159 });
160 return $dfv->check($self->query,$profile);
161 }
162
164 An input profile is a hash reference containing one or more of the
165 following keys.
166
167 Here is a very simple input profile. Examples of more advanced options
168 are described below.
169
170 use Data::FormValidator::Constraints qw(:closures);
171
172 my $profile = {
173 optional => [qw( company
174 fax
175 country )],
176
177 required => [qw( fullname
178 phone
179 email
180 address )],
181
182 constraint_methods => {
183 email => email(),
184 }
185 };
186
187 That defines some fields as optional, some as required, and defines
188 that the field named 'email' must pass the constraint named 'email'.
189
190 Here is a complete list of the keys available in the input profile,
191 with examples of each.
192
193 required
194 This is an array reference which contains the name of the fields which
195 are required. Any fields in this list which are not present or contain
196 only spaces will be reported as missing.
197
198 required_regexp
199 required_regexp => qr/city|state|zipcode/,
200
201 This is a regular expression used to specify additional field names for
202 which values will be required.
203
204 require_some
205 require_some => {
206 # require any two fields from this group
207 city_or_state_or_zipcode => [ 2, qw/city state zipcode/ ],
208 }
209
210 This is a reference to a hash which defines groups of fields where 1 or
211 more fields from the group should be required, but exactly which fields
212 doesn't matter. The keys in the hash are the group names. These are
213 returned as "missing" unless the required number of fields from the
214 group has been filled in. The values in this hash are array references.
215 The first element in this array should be the number of fields in the
216 group that is required. If the first field in the array is not an a
217 digit, a default of "1" will be used.
218
219 optional
220 optional => [qw/meat coffee chocolate/],
221
222 This is an array reference which contains the name of optional fields.
223 These are fields which MAY be present and if they are, they will be
224 checked for valid input. Any fields not in optional or required list
225 will be reported as unknown.
226
227 optional_regexp
228 optional_regexp => qr/_province$/,
229
230 This is a regular expression used to specify additional fields which
231 are optional. For example, if you wanted all fields names that begin
232 with user_ to be optional, you could use the regular expression,
233 /^user_/
234
235 dependencies
236 dependencies => {
237
238 # If cc_no is entered, make cc_type and cc_exp required
239 "cc_no" => [ qw( cc_type cc_exp ) ],
240
241 # if pay_type eq 'check', require check_no
242 "pay_type" => {
243 check => [ qw( check_no ) ],
244 }
245
246 # if cc_type is VISA or MASTERCARD require CVV
247 "cc_type" => sub {
248 my $dfv = shift;
249 my $type = shift;
250
251 return [ 'cc_cvv' ] if ($type eq "VISA" || $type eq "MASTERCARD");
252 return [ ];
253 },
254 },
255
256 This is for the case where an optional field has other requirements.
257 The dependent fields can be specified with an array reference.
258
259 If the dependencies are specified with a hash reference then the
260 additional constraint is added that the optional field must equal a key
261 for the dependencies to be added.
262
263 If the dependencies are specified as a code reference then the code
264 will be executed to determine the dependent fields. It is passed two
265 parameters, the object and the value of the field, and it should return
266 an array reference containing the list of dependent fields.
267
268 Any fields in the dependencies list that are missing when the target is
269 present will be reported as missing.
270
271 dependency_groups
272 dependency_groups => {
273 # if either field is filled in, they all become required
274 password_group => [qw/password password_confirmation/],
275 }
276
277 This is a hash reference which contains information about groups of
278 interdependent fields. The keys are arbitrary names that you create and
279 the values are references to arrays of the field names in each group.
280
281 dependencies_regexp
282 dependencies_regexp => {
283 qr/Line\d+\_ItemType$/ => sub {
284 my $dfv = shift;
285 my $itemtype = shift;
286 my $field = shift;
287
288 if ($type eq 'NeedsBatteries') {
289 my ($prefix, $suffix) = split(/\_/, $field);
290
291 return([$prefix . '_add_batteries]);
292 } else {
293 return([]);
294 }
295 },
296 },
297
298 This is a regular expression used to specify additional fields which
299 are dependent. For example, if you wanted to add dependencies for all
300 fields which meet a certain criteria (such as multiple items in a
301 shopping cart) where you do not know before hand how many of such
302 fields you may have.
303
304 dependent_optionals
305 dependent_optionals => {
306 # If delivery_address is specified then delivery_notes becomes optional
307 "delivery_address" => [ qw( delivery_notes ) ],
308
309 # if delivery_type eq 'collection', collection_notes becomes optional
310 "delivery_type" => {
311 collection => [ qw( collection_notes ) ],
312 }
313
314 # if callback_type is "phone" or "email" then additional_notes becomes optional
315 "callback_type" => sub {
316 my $dfv = shift;
317 my $type = shift;
318
319 if ($type eq 'phone' || $type eq 'email') {
320 return(['additional_notes']);
321 } else {
322 return([]);
323 }
324 },
325 },
326
327 This is for the case where an optional field can trigger other optional
328 fields. The dependent optional fields can be specified with an array
329 reference.
330
331 If the dependent optional fields are specified with a hash reference,
332 then an additional constraint is added that the optional field must
333 equal a key for the additional optional fields to be added.
334
335 If the dependent optional fields are specified as a code reference then
336 the code will be executed to determine the additional optional fields.
337 It is passed two parameters, the object and the value of the field, and
338 it should return an array reference containing the list of additional
339 optional fields.
340
341 dependent_require_some
342 dependent_require_some => {
343 # require any fields from this group if AddressID is "new"
344 AddressID => sub {
345 my $dfv = shift;
346 my $value = shift;
347
348 if ($value eq 'new') {
349 return({
350 house_name_or_number => [ 1, 'HouseName', 'HouseNumber' ],
351 });
352 } else {
353 return;
354 }
355 },
356 }
357
358 Sometimes a field will need to trigger additional dependencies but you
359 only require some of the fields. You cannot set them all to be
360 dependent as you might only have some of them, and you cannot set them
361 all to be optional as you must have some of them. This method allows
362 you to specify this in a similar way to the equire_some method but
363 dependent upon other values. In the example above if the AddressID
364 submitted is "new" then at least 1 of HouseName and HouseNumber must
365 also be supplied. See require_some for the valid options for the
366 return.
367
368 defaults
369 defaults => {
370 country => "USA",
371 },
372
373 This is a hash reference where keys are field names and values are
374 defaults to use if input for the field is missing.
375
376 The values can be code refs which will be used to calculate the value
377 if needed. These code refs will be passed in the DFV::Results object as
378 the only parameter.
379
380 The defaults are set shortly before the constraints are applied, and
381 will be returned with the other valid data.
382
383 defaults_regexp_map
384 defaults_regexp_map => {
385 qr/^opt_/ => 1,
386 },
387
388 This is a hash reference that maps regular expressions to default
389 values to use for matching optional or required fields.
390
391 It's useful if you have generated many checkbox fields with the similar
392 names. Since checkbox fields submit nothing at all when they are not
393 checked, it's useful to set defaults for them.
394
395 Note that it doesn't make sense to use a default for a field handled by
396 "optional_regexp" or "required_regexp". When the field is not
397 submitted, there is no way to know that it should be optional or
398 required, and thus there's no way to know that a default should be set
399 for it.
400
401 filters
402 # trim leading and trailing whitespace on all fields
403 filters => ['trim'],
404
405 This is a reference to an array of filters that will be applied to ALL
406 optional and required fields, before any constraints are applied.
407
408 This can be the name of a built-in filter (trim,digit,etc) or an
409 anonymous subroutine which should take one parameter, the field value
410 and return the (possibly) modified value.
411
412 Filters modify the data returned through the results object, so use
413 them carefully.
414
415 See Data::FormValidator::Filters for details on the built-in filters.
416
417 field_filters
418 field_filters => {
419 cc_no => ['digit'],
420 },
421
422 A hash ref with field names as keys. Values are array references of
423 built-in filters to apply (trim,digit,etc) or an anonymous subroutine
424 which should take one parameter, the field value and return the
425 (possibly) modified value.
426
427 Filters are applied before any constraints are applied.
428
429 See Data::FormValidator::Filters for details on the built-in filters.
430
431 field_filter_regexp_map
432 field_filter_regexp_map => {
433 # Upper-case the first letter of all fields that end in "_name"
434 qr/_name$/ => ['ucfirst'],
435 },
436
437 'field_filter_regexp_map' is used to apply filters to fields that match
438 a regular expression. This is a hash reference where the keys are the
439 regular expressions to use and the values are references to arrays of
440 filters which will be applied to specific input fields. Just as with
441 'field_filters', you can you use a built-in filter or use a coderef to
442 supply your own.
443
444 constraint_methods
445 use Data::FormValidator::Constraints qw(:closures);
446
447 constraint_methods => {
448 cc_no => cc_number({fields => ['cc_type']}),
449 cc_type => cc_type(),
450 cc_exp => cc_exp(),
451 },
452
453 A hash ref which contains the constraints that will be used to check
454 whether or not the field contains valid data.
455
456 Note: To use the built-in constraints, they need to first be loaded
457 into your name space using the syntax above. (Unless you are using the
458 old "constraints" key, documented in "BACKWARDS COMPATIBILITY").
459
460 The keys in this hash are field names. The values can be any of the
461 following:
462
463 o A named constraint.
464
465 Example:
466
467 my_zipcode_field => zip(),
468
469 See Data::FormValidator::Constraints for the details of which
470 built-in constraints that are available.
471
472 o A perl regular expression
473
474 Example:
475
476 my_zipcode_field => qr/^\d{5}$/, # match exactly 5 digits
477
478 If this field is named in "untaint_constraint_fields" or
479 "untaint_regexp_map", or "untaint_all_constraints" is effective, be
480 aware of the following: If you write your own regular expressions
481 and only match part of the string then you'll only get part of the
482 string in the valid hash. It is a good idea to write you own
483 constraints like /^regex$/. That way you match the whole string.
484
485 o a subroutine reference, to supply custom code
486
487 This will check the input and return true or false depending on the
488 input's validity. By default, the constraint function receives a
489 Data::FormValidator::Results object as its first argument, and the
490 value to be validated as the second. To validate a field based on
491 more inputs than just the field itself, see "VALIDATING INPUT BASED
492 ON MULTIPLE FIELDS".
493
494 Examples:
495
496 # Notice the use of 'pop'--
497 # the object is the first arg passed to the method
498 # while the value is the second, and last arg.
499 my_zipcode_field => sub { my $val = pop; return $val =~ '/^\d{5}$/' },
500
501 # OR you can reference a subroutine, which should work like the one above
502 my_zipcode_field => \&my_validation_routine,
503
504 # An example of setting the constraint name.
505 my_zipcode_field => sub {
506 my ($dfv, $val) = @_;
507 $dfv->set_current_constraint_name('my_constraint_name');
508 return $val =~ '/^\d{5}$/'
509 },
510
511 o an array reference
512
513 An array reference is used to apply multiple constraints to a
514 single field. Any of the above options are valid entries the array.
515 See "MULTIPLE CONSTRAINTS" below.
516
517 For more details see "VALIDATING INPUT BASED ON MULTIPLE FIELDS".
518
519 constraint_method_regexp_map
520 use Data::FormValidator::Constraints qw(:closures);
521
522 # In your profile.
523 constraint_method_regexp_map => {
524 # All fields that end in _postcode have the 'postcode' constraint applied.
525 qr/_postcode$/ => postcode(),
526 },
527
528 A hash ref where the keys are the regular expressions to use and the
529 values are the constraints to apply.
530
531 If one or more constraints have already been defined for a given field
532 using "constraint_methods", "constraint_method_regexp_map" will add an
533 additional constraint for that field for each regular expression that
534 matches.
535
536 untaint_all_constraints
537 untaint_all_constraints => 1,
538
539 If this field is set, all form data that passes a constraint will be
540 untainted. The untainted data will be returned in the valid hash.
541 Untainting is based on the pattern match used by the constraint. Note
542 that some constraint routines may not provide untainting.
543
544 See Writing your own constraint routines for more information.
545
546 This is overridden by "untaint_constraint_fields" and
547 "untaint_regexp_map".
548
549 untaint_constraint_fields
550 untaint_constraint_fields => [qw(zipcode state)],
551
552 Specifies that one or more fields will be untainted if they pass their
553 constraint(s). This can be set to a single field name or an array
554 reference of field names. The untainted data will be returned in the
555 valid hash.
556
557 This overrides the untaint_all_constraints flag.
558
559 untaint_regexp_map
560 untaint_regexp_map => [qr/some_field_\d/],
561
562 Specifies that certain fields will be untainted if they pass their
563 constraints and match one of the regular expressions supplied. This can
564 be set to a single regex, or an array reference of regexes. The
565 untainted data will be returned in the valid hash.
566
567 The above example would untaint the fields named "some_field_1", and
568 "some_field_2" but not "some_field".
569
570 This overrides the untaint_all_constraints flag.
571
572 missing_optional_valid
573 missing_optional_valid => 1
574
575 This can be set to a true value to cause optional fields with empty
576 values to be included in the valid hash. By default they are not
577 included-- this is the historical behavior.
578
579 This is an important flag if you are using the contents of an "update"
580 form to update a record in a database. Without using the option, fields
581 that have been set back to "blank" may fail to get updated.
582
583 validator_packages
584 # load all the constraints and filters from these modules
585 validator_packages => [qw(Data::FormValidator::Constraints::Upload)],
586
587 This key is used to define other packages which contain constraint
588 routines or filters. Set this key to a single package name, or an
589 arrayref of several. All of its constraint and filter routines
590 beginning with 'match_', 'valid_' and 'filter_' will be imported into
591 Data::FormValidator. This lets you reference them in a constraint with
592 just their name, just like built-in routines. You can even override
593 the provided validators.
594
595 See Writing your own constraint routines documentation for more
596 information
597
598 msgs
599 This key is used to define parameters related to formatting error
600 messages returned to the user.
601
602 By default, invalid fields have the message "Invalid" associated with
603 them while missing fields have the message "Missing" associated with
604 them.
605
606 In the simplest case, nothing needs to be defined here, and the default
607 values will be used.
608
609 The default formatting applied is designed for display in an XHTML web
610 page. That formatting is as followings:
611
612 <span style="color:red;font-weight:bold" class="dfv_errors">* %s</span>
613
614 The %s will be replaced with the message. The effect is that the
615 message will appear in bold red with an asterisk before it. This style
616 can be overridden by simply defining "dfv_errors" appropriately in a
617 style sheet, or by providing a new format string.
618
619 Here's a more complex example that shows how to provide your own
620 default message strings, as well as providing custom messages per
621 field, and handling multiple constraints:
622
623 msgs => {
624
625 # set a custom error prefix, defaults to none
626 prefix=> 'error_',
627
628 # Set your own "Missing" message, defaults to "Missing"
629 missing => 'Not Here!',
630
631 # Default invalid message, default's to "Invalid"
632 invalid => 'Problematic!',
633
634 # message separator for multiple messages
635 # Defaults to ' '
636 invalid_separator => ' <br /> ',
637
638 # formatting string, default given above.
639 format => 'ERROR: %s',
640
641 # Error messages, keyed by constraint name
642 # Your constraints must be named to use this.
643 constraints => {
644 'date_and_time' => 'Not a valid time format',
645 # ...
646 },
647
648 # This token will be included in the hash if there are
649 # any errors returned. This can be useful with templating
650 # systems like HTML::Template
651 # The 'prefix' setting does not apply here.
652 # defaults to undefined
653 any_errors => 'some_errors',
654 }
655
656 The hash that's prepared can be retrieved through the "msgs" method
657 described in the Data::FormValidator::Results documentation.
658
659 msgs - callback
660 This is a new feature. While it expected to be forward-compatible, it
661 hasn't yet received the testing the rest of the API has.
662
663 If the built-in message generation doesn't suit you, it is also
664 possible to provide your own by specifying a code reference:
665
666 msgs => \&my_msgs_callback
667
668 This will be called as a Data::FormValidator::Results method. It may
669 receive as arguments an additional hash reference of control
670 parameters, corresponding to the key names usually used in the "msgs"
671 area of the profile. You can ignore this information if you'd like.
672
673 If you have an alternative error message handler you'd like to share,
674 stick in the "Data::FormValidator::ErrMsgs" name space and upload it to
675 CPAN.
676
677 debug
678 This method is used to print details about what is going on to STDERR.
679
680 Currently only level '1' is used. It provides information about which
681 fields matched constraint_regexp_map.
682
683 A shortcut for array refs
684 A number of parts of the input profile specification include array
685 references as their values. In any of these places, you can simply use
686 a string if you only need to specify one value. For example, instead of
687
688 filters => [ 'trim' ]
689
690 you can simply say
691
692 filters => 'trim'
693
694 A note on regular expression formats
695 In addition to using the preferred method of defining regular
696 expressions using "qr", a deprecated style of defining them as strings
697 is also supported.
698
699 Preferred:
700
701 qr/this is great/
702
703 Deprecated, but supported
704
705 'm/this still works/'
706
708 You can pass more than one value into a constraint routine. For that,
709 the value of the constraint should be a hash reference. If you are
710 creating your own routines, be sure to read the section labeled
711 "WRITING YOUR OWN CONSTRAINT ROUTINES", in the
712 Data::FormValidator::Constraints documentation. It describes a newer
713 and more flexible syntax.
714
715 Using the original syntax, one key should be named "constraint" and
716 should have a value set to the reference of the subroutine or the name
717 of a built-in validator. Another required key is "params". The value
718 of the "params" key is a reference to an array of the other elements to
719 use in the validation. If the element is a scalar, it is assumed to be
720 a field name. The field is known to Data::FormValidator, the value will
721 be filtered through any defined filters before it is passed in. If the
722 value is a reference, the reference is passed directly to the routine.
723 Don't forget to include the name of the field to check in that list, if
724 you are using this syntax.
725
726 Example:
727
728 cc_no => {
729 constraint => "cc_number",
730 params => [ qw( cc_no cc_type ) ],
731 },
732
734 Multiple constraints can be applied to a single field by defining the
735 value of the constraint to be an array reference. Each of the values in
736 this array can be any of the constraint types defined above.
737
738 When using multiple constraints it is important to return the name of
739 the constraint that failed so you can distinguish between them. To do
740 that, either use a named constraint, or use the hash ref method of
741 defining a constraint and include a "name" key with a value set to the
742 name of your constraint. Here's an example:
743
744 my_zipcode_field => [
745 'zip',
746 {
747 constraint_method => '/^406/',
748 name => 'starts_with_406',
749 }
750 ],
751
752 You can use an array reference with a single constraint in it if you
753 just want to have the name of your failed constraint returned in the
754 above fashion.
755
756 Read about the "validate()" function above to see how multiple
757 constraints are returned differently with that method.
758
760 For even more advanced validation, you will likely want to read the
761 documentation for other modules in this distribution, linked below.
762 Also keep in mind that the Data::FormValidator profile structure is
763 just another data structure. There is no reason why it needs to be
764 defined statically. The profile could also be built on the fly with
765 custom Perl code.
766
768 validate()
769 my( $valids, $missings, $invalids, $unknowns ) =
770 Data::FormValidator->validate( \%input_hash, \%dfv_profile);
771
772 "validate()" provides a deprecated alternative to "check()". It has the
773 same input syntax, but returns a four element array, described as
774 follows
775
776 valids
777 This is a hash reference to the valid fields which were submitted
778 in the data. The data may have been modified by the various filters
779 specified.
780
781 missings
782 This is a reference to an array which contains the name of the
783 missing fields. Those are the fields that the user forget to fill
784 or filled with spaces. These fields may comes from the required
785 list or the dependencies list.
786
787 invalids
788 This is a reference to an array which contains the name of the
789 fields which failed one or more of their constraint checks.
790
791 Fields defined with multiple constraints will have an array ref
792 returned in the @invalids array instead of a string. The first
793 element in this array is the name of the field, and the remaining
794 fields are the names of the failed constraints.
795
796 unknowns
797 This is a list of fields which are unknown to the profile. Whether
798 or not this indicates an error in the user input is application
799 dependent.
800
801 constraints (profile key)
802 This is a supported but deprecated profile key. Using
803 "constraint_methods" is recommended instead, which provides a simpler,
804 more versatile interface.
805
806 constraints => {
807 cc_no => {
808 constraint => "cc_number",
809 params => [ qw( cc_no cc_type ) ],
810 },
811 cc_type => "cc_type",
812 cc_exp => "cc_exp",
813 },
814
815 A hash ref which contains the constraints that will be used to check
816 whether or not the field contains valid data.
817
818 The keys in this hash are field names. The values can be any of the
819 following:
820
821 o A named constraint.
822
823 Example:
824
825 my_zipcode_field => 'zip',
826
827 See Data::FormValidator::Constraints for the details of which
828 built-in constraints that are available.
829
830 hashref style of specifying constraints
831 Using a hash reference to specify a constraint is an older technique
832 used to name a constraint or supply multiple parameters.
833
834 Both of these interface issues are now better addressed with
835 "constraint_methods" and "$self-\"name_this('foo')>.
836
837 # supply multiple parameters
838 cc_no => {
839 constraint => "cc_number",
840 params => [ qw( cc_no cc_type ) ],
841 },
842
843 # name a constraint, useful for returning error messages
844 last_name => {
845 name => "ends_in_name",
846 constraint => qr/_name$/,
847 },
848
849 Using a hash reference for a constraint permits the passing of multiple
850 arguments. Required arguments are "constraint" or "constraint_method".
851 Optional arguments are "name" and "params".
852
853 A "name" on a constraints 'glues' the constraint to its error message
854 in the validator profile (refer "msgs" section below). If no "name" is
855 given then it will default to the value of "constraint" or
856 "constraint_method" IF they are NOT a CODE ref or a RegExp ref.
857
858 The "params" value is a reference to an array of the parameters to pass
859 to the constraint method. If an element of the "params" list is a
860 scalar, it is assumed to be naming a key of the %input_hash and that
861 value is passed to the routine. If the parameter is a reference, then
862 it is treated literally and passed unchanged to the routine.
863
864 If you are using the older "constraint" over the new
865 "constraint_method" then don't forget to include the name of the field
866 to check in the "params" list. "constraint_method" provides access to
867 this value via the "get_current_*" methods (refer
868 Data::FormValidator::Constraints)
869
870 For more details see "VALIDATING INPUT BASED ON MULTIPLE FIELDS".
871
872 constraint_regexp_map (profile key)
873 This is a supported but deprecated profile key. Using
874 "constraint_methods_regexp_map" is recommended instead.
875
876 constraint_regexp_map => {
877 # All fields that end in _postcode have the 'postcode' constraint applied.
878 qr/_postcode$/ => 'postcode',
879 },
880
881 A hash ref where the keys are the regular expressions to use and the
882 values are the constraints to apply.
883
884 If one or more constraints have already been defined for a given field
885 using "constraints", constraint_regexp_map will add an additional
886 constraint for that field for each regular expression that matches.
887
889 Other modules in this distribution:
890
891 Data::FormValidator::Constraints
892
893 Data::FormValidator::Constraints::Dates
894
895 Data::FormValidator::Constraints::Upload
896
897 Data::FormValidator::ConstraintsFactory
898
899 Data::FormValidator::Filters
900
901 Data::FormValidator::Results
902
903 A sample application by the maintainer:
904
905 Validating Web Forms with Perl,
906 <http://mark.stosberg.com/Tech/perl/form-validation/>
907
908 Related modules:
909
910 Data::FormValidator::Tutorial
911
912 Data::FormValidator::Util::HTML
913
914 CGI::Application::ValidateRM, a CGI::Application & Data::FormValidator
915 glue module
916
917 HTML::Template::Associate::FormValidator is designed to make some kinds
918 of integration with HTML::Template easier.
919
920 Params::Validate is useful for validating function parameters.
921
922 Regexp::Common, Data::Types, Data::Verify, Email::Valid,
923 String::Checker, CGI::ArgChecker, CGI::FormMagick::Validator,
924 CGI::Validate
925
926 Document Translations:
927
928 Japanese: <http://perldoc.jp/docs/modules/>
929
930 Distributions which include Data::FormValidator
931
932 FreeBSD includes a port named p5-Data-FormValidator
933
934 Debian GNU/Linux includes a port named libdata-formvalidator-perl
935
937 Some of these input validation functions have been taken from MiniVend
938 by Michael J. Heins.
939
940 The credit card checksum validation was taken from contribution by
941 Bruce Albrecht to the MiniVend program.
942
944 Bug reports and patches are welcome. Reports which include a failing
945 Test::More style test are helpful and will receive priority.
946
947 <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-FormValidator>
948
950 This project is maintained on Github
951 <https://github.com/dnmfarrell/Data-FormValidator>.
952
954 Currently maintained by David Farrell <dfarrell@cpan.org>
955
956 Parts Copyright 2001-2006 by Mark Stosberg <mark at summersault.com>,
957 (previous maintainer)
958
959 Copyright (c) 1999 Francis J. Lacoste and iNsu Innovations Inc. All
960 rights reserved. (Original Author)
961
962 Parts Copyright 1996-1999 by Michael J. Heins <mike@heins.net>
963
964 Parts Copyright 1996-1999 by Bruce Albrecht
965 <bruce.albrecht@seag.fingerhut.com>
966
968 This program is free software; you can redistribute it and/or modify it
969 under the terms as perl itself.
970
971
972
973perl v5.32.1 2021-01-27 Data::FormValidator(3)