Embperl - building dynamic websites with Perl


DATA STRUCTURES
[ << Prev: METHODS ] [ Content ] [ Next: Example >> ]

The functions and methods expect the named data structures as follows:



RULEStop

The $rules 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.

  [
    [
    -key        => 'lang',
    -name       => 'Language'
    required    => 1,
    length_max  => 5,
    ],
    [
    -key        => 'from',
    -type       => 'EMail',
    emptyok     => 1,
    ],

    -key        => ['foo', 'bar']
    required    => 1,
  ]   

All items starting with a dash are control elements, while all items without a dash are tests to perform.

 

-key

 

gives the key in the passed form data hash which should be tested. -key is normally the name given in the HTML name attribute within a form field. -key can also be a arrayref, in which case only one of the given keys must satisfy the following test to succeed.

 

-key_break

 

same as -key and -break => 1 without reseting name -name and -msg.

 

-name

 

is a human readable name that should be used in error messages. Can be hash with multiple languages, e.g.

    -name => { 'en' => 'date', 'de' => 'Datum' }
 

-type

 

specfify to not use the standard tests, but the ones for a special type. For example there is a type Number 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::<Typename>. The -type directive also can verify that the given data has a valid format for the type.

The following types are available:

 

Default

 

This one is used when no type is specified. It contains all the standard tests.

 

Number

 

Input must be a floating point number.

 

Integer

 

Input must be a integer number.

 

PosInteger

 

Input must be a integer number and greater or equal zero.

 

TimeHHMM

 

Input must be the time in the format hh::mm

 

TimeHHMMSS

 

Input must be the time in the format hh::mm:ss

 

TimeValue

 

Input must be a number followed by s, m, h, d or w.

 

EMail

 

Input must be a valid email address including a top level domain e.g. user@example.com

 

EMailRFC

 

Input must be a valid email address, no top level domain is required, so user@foo is also valid.

 

IPAddr

 

Input must be an ip-address in the form nnn.nnn.nnn.nnn

 

IPAddr_Mask

 

Input must be an ip-address and network mask in the form nnn.nnn.nnn.nnn/mm

 

FQDN_IPAddr

 

Input must be an ip-address or an fqdn (host.domain)

 

select

 

This used together with required and causes Embperl::Form::Validate to test of a selected index != 0 instead of a non empty input.

If you write your own type package, make sure to send them back, so they can be part of the next distribution.

 

-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.

       [
	-key        => 'email',
	-name       => 'E-Mail-Address',
	emptyok     => 1,                   # it's ok to leave this field empty (in this case the following tests are skipped)
         
	-msg => 'The E-Mail-Address is invalid.',
	matches_regex => '(^[^ <>()@¡-ÿ]+@[^ <>()@¡-ÿ]+\.[a-zA-Z]{2,3}$)', 
        	
	-msg => 'The E-Mail address must contain a "@".',
	must_contain_one_of => '@',
         
	-msg => 'The E-Mail address must contain at least one period.',
	must_contain_one_of => '.',
       ],
 

-fail

 

stops further validation of any rule after the first error is found

 

-cont

 

continues validation in the same group, also a error was found

 

-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:

    [
    -key => 'action',  emptyok => 1, -break => 1, ne => 0, -break => 0,
    -key => 'input', 'required' => 1
    ]

The above example will only require the field "input", when the field "action" is not empty and is not zero.

 

-key_check, -key_end

 

Is used for preconditions, same example as for -break

    [
    -key => 'input', 
    -key_check => 'action', emptyok => 1, ne => 0, -key_end,
    'required' => 1
    ]

The above example will only require the field "input", when the field "action" is not empty and is not zero.

 

backend_only

 

The following rules will only executed in the backend

 

frontend_only

 

The following rules will only executed in the fronend

 

[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.

The following test are currently defined:

 

required

 

emptyok

 

length_min

 

length_max

 

length_eq

 

eq

 

same

 

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:

  $epf = Embperl::Form::Validate -> new (
        [
            -key => 'pass',  -name => 'Password', required => 1, length_min => 4,
            -key => 'pass2', -name => 'Repeat Password', required => 1, length_min => 4,
                             same => 'pass:Password',
        ],
        'passform') ; 
 

ne

 

lt

 

gt

 

le

 

ge

 

matches_regex

 

Value must match Perl regular expression. Only executed on server side.

 

matches_regex_js

 

Value must match JavaScript regular expression. Only executed on client side. IMPORTANT: If the user has disabled JavaScript in his browser this test will be never executed. Use a corresponding Perl Regex with matches_regex to get a server side validation. Use this with care, because different browser may have different support for regular expressions.

 

not_matches_regex

 

Value must not match Perl regular expression. Only executed on server side.

 

not_matches_regex_js

 

Value must not match JavaScript regular expression. Only executed on client side. IMPORTANT: If the user has disabled JavaScript in his browser this test will be never executed. Use a corresponding Perl Regex with not_matches_regex to get a server side validation. Use this with care, because different browser may have different support for regular expressions.

 

matches_wildcard

 

must_only_contain

 

must_not_contain

 

must_contain_one_of

 

checked

 

Checkbox must be selected

 

notchecked

 

Checkbox must not be selected



PREFERENCEStop

The $pref 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



ERROR CODEStop

For a descriptions of the error codes, validate is returning see validate



FDATtop

See also Embperl.

 my $fdat = { foo => 'foobar',
	      bar => 'baz', 
	      baz => 49, 
	      fnord => 1.2 };

[ << Prev: METHODS ] [ Content ] [ Next: Example >> ]


© 1997-2023 Gerald Richter / actevy