.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Form::Validate 3pm" .TH Form::Validate 3pm "2020-11-09" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Embperl::Form::Validate \- Form validation with server\- and client\-side support. .SH "DESCRIPTION" .IX Header "DESCRIPTION" This modules is developed to do form validation for you. It works on the server side by checking the posted form data and it generates client side script functions, to validate the form values, as far as possible, before they are send to the server, to avoid another server roundtrip. .PP Also it has the best support for Embperl, it should also work outside of Embperl e.g. with \s-1CGI\s0.pm or mod_perl. .PP It can be extended by new validation rules for additional syntaxes (e.g. \s-1US\s0 zip codes, German Postleitzahlen, number plates, iso\-3166 2\-digit language or country codes, etc.) .PP Each module has the ability to rely it's answer on parameters like e.g. the browser, which caused the request for or submitted the form. .PP The module fully supports internationalisation. Any message can be provided in multiple languages and it makes use of Embperl's multilanguage support. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Embperl::Form::Validate; \& \& my $epf = new Embperl::Form::Validate($rules, $form_id); \& \& $epf\->add_rule(\*(Aqfnord\*(Aq, $fnord_rules); \& \& # validate the form values and returns error information, if any \& my $result = $epf \-> validate ; \& \& # Does the form content validate? \& print \*(AqValidate: \*(Aq . ($result?\*(Aqno\*(Aq:\*(Aqyes\*(Aq); \& \& # validate the form values and reaturn all error messages, if any \& my $errors = $epf\->validate_messages($fdat, $pref); \& \& # Get the code for a client\-side form validation according to the \& # rules given to new: \& $epf \-> get_script_code ; .Ve .SH "METHODS" .IX Header "METHODS" The following methods are available: .ie n .SS "$epf = Embperl::Form::Validate \-> new ($rules [, $form_id ], [$default_language], [$charset]);" .el .SS "\f(CW$epf\fP = Embperl::Form::Validate \-> new ($rules [, \f(CW$form_id\fP ], [$default_language], [$charset]);" .IX Subsection "$epf = Embperl::Form::Validate -> new ($rules [, $form_id ], [$default_language], [$charset]);" Constructor for a new form validator. Returns a reference to a Embperl::Form::Validate object. .ie n .IP "$rules" 4 .el .IP "\f(CW$rules\fR" 4 .IX Item "$rules" should be a reference to an array of rules, see \*(L"\s-1RULES\*(R"\s0 elsewhere in this document for details. .ie n .IP "$form_id" 4 .el .IP "\f(CW$form_id\fR" 4 .IX Item "$form_id" should be the name (im \s-1HTML\s0) or id (in \s-1XHTML\s0) parameter of the form tag, which has to be verified.It\e's e.g. used for generating the right path in the JavaScript \s-1DOM.\s0 It defaults to 'forms[0]' which should be the first form in your page. .ie n .IP "$default_language" 4 .el .IP "\f(CW$default_language\fR" 4 .IX Item "$default_language" language to use when no messages are available in the desired language. Defaults to 'en'. .ie n .IP "$charset" 4 .el .IP "\f(CW$charset\fR" 4 .IX Item "$charset" Pass 'utf\-8' in case you want utf\-8 messages. .ie n .SS "$epf\->add_rules($field, $field_rules);" .el .SS "\f(CW$epf\fP\->add_rules($field, \f(CW$field_rules\fP);" .IX Subsection "$epf->add_rules($field, $field_rules);" Adds rules \f(CW$field_rules\fR for a (new) field \f(CW$field\fR to the validator, e.g. .PP .Vb 1 \& $epf\->add_rule([ \-key => \*(Aqfnord\*(Aq, \-type => \*(AqNumber\*(Aq, \-max => 1.3, \-name => \*(AqFnord\*(Aq ]); .Ve .PP The new rule will be appended to the end of the list of rules. .PP See \*(L"\s-1RULES\*(R"\s0 elsewhere in this document. .ie n .SS "$epf \-> validate ([$fdat, [$pref]]);" .el .SS "\f(CW$epf\fP \-> validate ([$fdat, [$pref]]);" .IX Subsection "$epf -> validate ([$fdat, [$pref]]);" Does the server-side form validation. .ie n .IP "$fdat" 4 .el .IP "\f(CW$fdat\fR" 4 .IX Item "$fdat" should be a hash reference to all postend form values. It defaults to \f(CW%fdat\fR of the current Embperl page. .ie n .IP "$pref" 4 .el .IP "\f(CW$pref\fR" 4 .IX Item "$pref" can contain additional information for the validation process. At the moment the keys \f(CW\*(C`language\*(C'\fR and \f(CW\*(C`default_language\*(C'\fR are recognized. \f(CW\*(C`language\*(C'\fR defaults to the language set by Embperl. \f(CW\*(C`default_language\*(C'\fR defaults to the one given with \f(CW\*(C`new\*(C'\fR. .PP The method verifies the content \f(CW$fdat\fR according to the rules given to the Embperl::Form::Validate constructor and added by the \fBadd_rule()\fR method and returns an array reference to error information. If there is no error it returns undef. Each element of the returned array contains a hash with the following keys: .IP "key" 4 .IX Item "key" key into \f(CW$fdat\fR which caused the error .IP "id" 4 .IX Item "id" message id .IP "typeobj" 4 .IX Item "typeobj" object reference to the Validate object which was used to validate the field .IP "name" 4 .IX Item "name" human readable name, if any. Maybe a hash with multiple languages. .IP "msg" 4 .IX Item "msg" field specific messages, if any. Maybe a hash with multiple languages. .IP "param" 4 .IX Item "param" array with parameters which should subsituted inside the message .ie n .SS "$epf \-> error_message ($err, [ $pref ])" .el .SS "\f(CW$epf\fP \-> error_message ($err, [ \f(CW$pref\fP ])" .IX Subsection "$epf -> error_message ($err, [ $pref ])" Converts one item returned by validate into a error message .ie n .IP "$err" 4 .el .IP "\f(CW$err\fR" 4 .IX Item "$err" Item returned by validate .ie n .IP "$pref" 4 .el .IP "\f(CW$pref\fR" 4 .IX Item "$pref" Preferences (see validate) .ie n .SS "$epf \-> validate_messages ($fdat, [ $pref ])" .el .SS "\f(CW$epf\fP \-> validate_messages ($fdat, [ \f(CW$pref\fP ])" .IX Subsection "$epf -> validate_messages ($fdat, [ $pref ])" Validate the form content and returns the error messages as array ref if any. See validate for details. .ie n .SS "$epf \-> get_script_code ([$pref])" .el .SS "\f(CW$epf\fP \-> get_script_code ([$pref])" .IX Subsection "$epf -> get_script_code ([$pref])" Returns the script code necessary to do the client-side validation. Put the result between <\s-1SCRIPT\s0> and tags inside your page. It will contain a function that is named \f(CW\*(C`epform_validate_ where is replaced by the form named you have passed to new. You should call this function in the \f(CW\*(C`onSubmit\*(C'\fR of your form. Example: .PP .Vb 3 \& \& \&
\& .... \&
.Ve .SH "DATA STRUCTURES" .IX Header "DATA STRUCTURES" The functions and methods expect the named data structures as follows: .SS "\s-1RULES\s0" .IX Subsection "RULES" The \f(CW$rules\fR array contains a list of tests to perform. Alls the given tests are process sequenzially. You can group tests together, so when one test fails the remaining tests of the same group are not processed and the processing continues in the next outer group with the next test. .PP .Vb 12 \& [ \& [ \& \-key => \*(Aqlang\*(Aq, \& \-name => \*(AqLanguage\*(Aq \& required => 1, \& length_max => 5, \& ], \& [ \& \-key => \*(Aqfrom\*(Aq, \& \-type => \*(AqEMail\*(Aq, \& emptyok => 1, \& ], \& \& \-key => [\*(Aqfoo\*(Aq, \*(Aqbar\*(Aq] \& required => 1, \& ] .Ve .PP All items starting with a dash are control elements, while all items without a dash are tests to perform. .IP "\-key" 4 .IX Item "-key" gives the key in the passed form data hash which should be tested. \-key is normally the name given in the \s-1HTML\s0 name attribute within a form field. \&\f(CW\*(C`\-key\*(C'\fR can also be a arrayref, in which case \fBonly one of\fR the given keys must satisfy the following test to succeed. .IP "\-name" 4 .IX Item "-name" is a human readable name that should be used in error messages. Can be hash with multiple languages, e.g. .Sp .Vb 1 \& \-name => { \*(Aqen\*(Aq => \*(Aqdate\*(Aq, \*(Aqde\*(Aq => \*(AqDatum\*(Aq } .Ve .IP "\-type" 4 .IX Item "-type" specfify to not use the standard tests, but the ones for a special type. For example there is a type \f(CW\*(C`Number\*(C'\fR which will replace all the comparisons by numeric ones instead of string comparisons. You may add your own types by writing a module that contains the necessary test and dropping it under Embperl::Form::Validate::. The \-type directive also can verify that the given data has a valid format for the type. .Sp The following types are available: .RS 4 .IP "Default" 4 .IX Item "Default" This one is used when no type is specified. It contains all the standard tests. .IP "Number" 4 .IX Item "Number" Input must be a floating point number. .IP "Integer" 4 .IX Item "Integer" Input must be a integer number. .IP "PosInteger" 4 .IX Item "PosInteger" Input must be a integer number and greater or equal zero. .IP "TimeHHMM" 4 .IX Item "TimeHHMM" Input must be the time in the format hh::mm .IP "TimeHHMMSS" 4 .IX Item "TimeHHMMSS" Input must be the time in the format hh::mm:ss .IP "TimeValue" 4 .IX Item "TimeValue" Input must be a number followed by s, m, h, d or w. .IP "EMail" 4 .IX Item "EMail" Input must be a valid email address including a top level domain e.g. user@example.com .IP "EMailRFC" 4 .IX Item "EMailRFC" Input must be a valid email address, no top level domain is required, so user@foo is also valid. .IP "IPAddr" 4 .IX Item "IPAddr" Input must be an ip-address in the form nnn.nnn.nnn.nnn .IP "IPAddr_Mask" 4 .IX Item "IPAddr_Mask" Input must be an ip-address and network mask in the form nnn.nnn.nnn.nnn/mm .IP "FQDN_IPAddr" 4 .IX Item "FQDN_IPAddr" Input must be an ip-address or an fqdn (host.domain) .IP "select" 4 .IX Item "select" This used together with required and causes Embperl::Form::Validate to test of a selected index != 0 instead of a non empty input. .RE .RS 4 .Sp If you write your own type package, make sure to send them back, so they can be part of the next distribution. .RE .IP "\-msg" 4 .IX Item "-msg" Used to give messages which should be used when the test fails. This message overrides the standard messages provided by Embperl::Form::Validate and by Embperl's message management. Can also be a hash with messages for multiple languages. The \-msg parameter must precede the test for which it should be displayed. You can have multiple different messages for different tests, e.g. .Sp .Vb 4 \& [ \& \-key => \*(Aqemail\*(Aq, \& \-name => \*(AqE\-Mail\-Address\*(Aq, \& emptyok => 1, # it\*(Aqs ok to leave this field empty (in this case the following tests are skipped) \& \& \-msg => \*(AqThe E\-Mail\-Address is invalid.\*(Aq, \& matches_regex => \*(Aq(^[^ <>()@¡\-ÿ]+@[^ <>()@¡\-ÿ]+\e.[a\-zA\-Z]{2,3}$)\*(Aq, \& \& \-msg => \*(AqThe E\-Mail address must contain a "@".\*(Aq, \& must_contain_one_of => \*(Aq@\*(Aq, \& \& \-msg => \*(AqThe E\-Mail address must contain at least one period.\*(Aq, \& must_contain_one_of => \*(Aq.\*(Aq, \& ], .Ve .IP "\-fail" 4 .IX Item "-fail" stops further validation of any rule after the first error is found .IP "\-cont" 4 .IX Item "-cont" continues validation in the same group, also a error was found .IP "\-break => 1" 4 .IX Item "-break => 1" errors only break current block, but does not display any message. \&\-break => 0 turns bak to normal behaviour. This can be used for preconditions: .Sp .Vb 4 \& [ \& \-key => \*(Aqaction\*(Aq, emptyok => 1, \-break => 1, ne => 0, \-break => 0, \& \-key => \*(Aqinput\*(Aq, \*(Aqrequired\*(Aq => 1 \& ] .Ve .Sp The above example will only require the field \*(L"input\*(R", when the field \*(L"action\*(R" is not empty and is not zero. .IP "[arrayref]" 4 .IX Item "[arrayref]" you can place a arrayref with tests at any point in the rules list. The array will be considered as a group and the default is the stop processing of a group as soon as the first error is found and continue with processing with the next rule in the next outer group. .PP The following test are currently defined: .IP "required" 4 .IX Item "required" .PD 0 .IP "emptyok" 4 .IX Item "emptyok" .IP "length_min" 4 .IX Item "length_min" .IP "length_max" 4 .IX Item "length_max" .IP "length_eq" 4 .IX Item "length_eq" .IP "eq" 4 .IX Item "eq" .IP "same" 4 .IX Item "same" .PD Value must be the same as in field given as argument. This is useful if you want for example verify that two passwords are the same. The Text displayed to the user for the second field may be added to the argument separated by a colon. Example: .Sp .Vb 7 \& $epf = Embperl::Form::Validate \-> new ( \& [ \& \-key => \*(Aqpass\*(Aq, \-name => \*(AqPassword\*(Aq, required => 1, length_min => 4, \& \-key => \*(Aqpass2\*(Aq, \-name => \*(AqRepeat Password\*(Aq, required => 1, length_min => 4, \& same => \*(Aqpass:Password\*(Aq, \& ], \& \*(Aqpassform\*(Aq) ; .Ve .IP "ne" 4 .IX Item "ne" .PD 0 .IP "lt" 4 .IX Item "lt" .IP "gt" 4 .IX Item "gt" .IP "le" 4 .IX Item "le" .IP "ge" 4 .IX Item "ge" .IP "matches_regex" 4 .IX Item "matches_regex" .PD Value must match \fBPerl\fR regular expression. Only executed on server side. .IP "matches_regex_js" 4 .IX Item "matches_regex_js" Value must match \fBJavaScript\fR regular expression. Only executed on client side. \&\fB\s-1IMPORTANT:\s0\fR If the user has disabled JavaScript in his browser this test will be never executed. Use a corresponding Perl Regex with \f(CW\*(C`matches_regex\*(C'\fR to get a server side validation. Use this with care, because different browser may have different support for regular expressions. .IP "not_matches_regex" 4 .IX Item "not_matches_regex" Value must not match \fBPerl\fR regular expression. Only executed on server side. .IP "not_matches_regex_js" 4 .IX Item "not_matches_regex_js" Value must not match \fBJavaScript\fR regular expression. Only executed on client side. \&\fB\s-1IMPORTANT:\s0\fR If the user has disabled JavaScript in his browser this test will be never executed. Use a corresponding Perl Regex with \f(CW\*(C`not_matches_regex\*(C'\fR to get a server side validation. Use this with care, because different browser may have different support for regular expressions. .IP "matches_wildcard" 4 .IX Item "matches_wildcard" .PD 0 .IP "must_only_contain" 4 .IX Item "must_only_contain" .IP "must_not_contain" 4 .IX Item "must_not_contain" .IP "must_contain_one_of" 4 .IX Item "must_contain_one_of" .IP "checked" 4 .IX Item "checked" .PD Checkbox must be selected .IP "notchecked" 4 .IX Item "notchecked" Checkbox must not be selected .SS "\s-1PREFERENCES\s0" .IX Subsection "PREFERENCES" The \f(CW$pref\fR hash (reference) contains information about a single form request or submission, e.g. the browser version, which made the request or submission and the language in which the error messages should be returned. See also validate .SS "\s-1ERROR CODES\s0" .IX Subsection "ERROR CODES" For a descriptions of the error codes, validate is returning see validate .SS "\s-1FDAT\s0" .IX Subsection "FDAT" See also Embperl. .PP .Vb 4 \& my $fdat = { foo => \*(Aqfoobar\*(Aq, \& bar => \*(Aqbaz\*(Aq, \& baz => 49, \& fnord => 1.2 }; .Ve .SH "Example" .IX Header "Example" This example simply validates the form input when you hit submit. If your input is correct, the form is redisplay with your input, otherwise the error message is shown. If you turn off JavaScript the validation is still done one the server-side. Any validation for which no JavaScript validation is defined (like regex matches), only the server-side validation is performed. .PP .Vb 3 \& \& \& [\- \& \& use Embperl::Form::Validate ; \& \& $epf = Embperl::Form::Validate \-> new ( \& [ \& [ \& \-key => \*(Aqname\*(Aq, \& \-name => \*(AqName\*(Aq, \& required => 1, \& length_min => 4, \& ], \& [ \& \-key => \*(Aqid\*(Aq, \& \-name => \*(AqId\*(Aq, \& \-type => \*(AqNumber\*(Aq, \& gt => 0, \& lt => 10, \& ], \& [ \& \-key => \*(Aqemail\*(Aq, \& \-msg => \*(AqThis is not a valid E\-Mail address\*(Aq, \& must_contain_one_of => \*(Aq@.\*(Aq, \& matches_regex => \*(Aq..+@..+\e\e...+\*(Aq, \& length_min => 8, \& ], \& [ \& \-key => \*(Aqmsg\*(Aq, \& \-name => \*(AqMessage\*(Aq, \& emptyok => 1, \& length_min => 10, \& ] \& ]) ; \& \& if ($fdat{check}) \& { \& $errors = $epf \-> validate_messages ; \& } \& \& \-] \& \& \& \& \&

Embperl Example \- Input Form Validation

\& \& [$if @$errors $] \&

Please correct the following errors

\& [$foreach $e (@$errors)$] \& [+ $e +]
\& [$endforeach$] \& [$else$] \&

Please enter your data

\& [$endif$] \& \&
\& \& \& \& \& \& \&
Name
Id (1\-9)
E\-Mail
Message
\&
\& \& \&


\& \& Embperl (c) 1997\-2010 G.Richter / ecos gmbh www.ecos.de \& \& \& .Ve .PP See also eg/x/formvalidation.htm .SH "SEE ALSO" .IX Header "SEE ALSO" See also Embperl. .SH "AUTHOR" .IX Header "AUTHOR" Axel Beckert (abe@ecos.de) Gerald Richter (richter at embperl dot org)