X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fp5-CGI-Ex;a=blobdiff_plain;f=lib%2FCGI%2FEx%2FValidate.pod;h=53a24b62b14f5b55c011a2f41d24c82cbe4f3a5b;hp=9311c05c41ceb24d4473e28d963316867588622b;hb=6ab8b2e8e8388d1a238148a1ee58e124855f3768;hpb=fdecaac30a1168ed894c46d61b6c95524ec62a4e diff --git a/lib/CGI/Ex/Validate.pod b/lib/CGI/Ex/Validate.pod index 9311c05..53a24b6 100644 --- a/lib/CGI/Ex/Validate.pod +++ b/lib/CGI/Ex/Validate.pod @@ -335,8 +335,8 @@ This section lists the available validation types. Multiple instances of the same type may be used for some validation types by adding a number to the type (ie match, match2, match232). Multiple instances are validated in sorted order. Types that allow multiple values are: -compare, custom, custom_js, equals, enum, match, required_if, sql, -type, validate_if, and replace (replace is a MODIFICATION TYPE). +compare, custom, custom_js, equals, match, required_if, sql, validate_if, +and replace (replace is a MODIFICATION TYPE). =over 4 @@ -366,22 +366,41 @@ validation and an error is added. { field => 'username', custom => sub { - my ($key, $val, $type, $field_val_hash) = @_; + my ($key, $val, $field_val_hash, $checktype, $form) = @_; # do something here return 0; }, + custom_error => '$name was not valid', + } + +Often it is desirable to specify a different message depending upon +the code passed to custom. To use a custom error message simply die +with the error message. Note that you will want to add a newline or +else perl will add the line number and file for you - +CGI::Ex::Validate will remove the trailing newline. + + { + field => 'username', + custom => sub { + my ($key, $val) = @_; + die "Custom error message 1\n" if $val eq '1'; + die "Custom error message 2\n" if $val eq '2'; + return 0; + }, + custom_error => '$name default custom error message', } =item C -Custom value - only available in JS. Allows for extra programming types. -May be a javascript function (if fully declared in javascript), a string containing -a javascript function (that will be eval'ed into a real function), -a boolean value pre-determined before calling validate, or may be -section of javascript that will be eval'ed (the last value of -the eval'ed javascript will determine if validation passed). A false response indicates -the value did not pass validation. A true response indicates that it did. See -the samples/validate_js_0_tests.html page for a sample of usages. +Custom value - only available in JS. Allows for extra programming +types. May be a javascript function (if fully declared in +javascript), a string containing a javascript function (that will be +eval'ed into a real function), a boolean value pre-determined before +calling validate, or may be section of javascript that will be eval'ed +(the last value of the eval'ed javascript will determine if validation +passed). A false response indicates the value did not pass +validation. A true response indicates that it did. See the +samples/validate_js_0_tests.html page for a sample of usages. { field => 'date', @@ -389,17 +408,31 @@ the samples/validate_js_0_tests.html page for a sample of usages. match => 'm|^\d\d\d\d/\d\d/\d\d$|', match_error => 'Please enter date in YYYY/MM/DD format', custom_js => "function (args) { - var t=new Date(); - var y=t.getYear()+1900; - var m=t.getMonth() + 1; - var d=t.getDate(); - if (m<10) m = '0'+m; - if (d<10) d = '0'+d; + var t = new Date(); + var y = t.getYear()+1900; + var m = t.getMonth() + 1; + var d = t.getDate(); + if (m < 10) m = '0'+m; + if (d < 10) d = '0'+d; (args.value > ''+y+'/'+m+'/'+d) ? 1 : 0; }", custom_js_error => 'The date was not greater than today.', } +Often it is desirable to specify a different message depending upon +the function passed to custom_js. To use a custom error message simply throw +the error message. + + { + field => 'username', + custom_js => 'function (args) { + if (args.value == 1) throw "Custom error message 1"; + if (args.value == 2) throw "Custom error message 2"; + return 0; + }', + custom_js_error => '$name default custom error message', + } + =item C Allows for checking whether an item matches a set of options. In perl @@ -483,7 +516,11 @@ to allow more than one item by any given name). =item C Requires the form field to have some value. If the field is not present, -no other checks will be run. +no other checks will be run and an error will be given. + +It has been common for code to try C 0> which essentially has +no effect - instead use C 'fieldname', required => 1>. This +results in the fieldname only being required if the fieldname is present. =item C @@ -496,6 +533,9 @@ as saying: required_if => 'some_condition', +It is different in that other checks will run - whereas validate_if skips +all validation if some condition is not met. + If a regex is used for the field name, the required_if field will have any match patterns swapped in. @@ -524,15 +564,67 @@ $self->{dbh} is a coderef - they will be called and should return a dbh. =item C Allows for more strict type checking. Currently supported types -include CC (credit card), EMAIL, DOMAIN, IP, URL. Other types will be -added upon request provided we can add a perl and a javascript -version. +include CC (credit card), EMAIL, DOMAIN, IP, URL, INT, UINT, and NUM. +Other types will be added upon request provided we can add a perl and +a javascript version (extra types often aren't necessary as the custom and +custom_js options give arbitrary checking). If a type checks fails - other +compare, custom, or length checks will not be ran. { field => 'credit_card', type => 'CC', } +=over 4 + +item C + +Simple Luhn-10 check. Note that spaces and dashes are left in place. + +=item C + +Very basic check to see if the value looks like an address. The local part +must only contain [\w.~!\#\$%\^&*\-=+?] and the domain must be a domain or ip. +If you want full fledged RFC compliant checking consider something like: + + { + field => 'email', + custom => sub { + my ($key, $val, $fv, $type, $form) = @_; + require Mail::Address; + my @a = Mail::Address->parse($val); + die "Invalid address\n" if @a != 1; + return $form->{$key} = $a[0]->address; + }, + } + +=item C + +Checks for a valid domain name - does no lookup of the domain. For that use +a custom sub. + +=item C + +Basic IPv4 check. + +=item C + +Basic check that string matches something resembling an http or https url. + +=item C + +Checks for an integer between -2147483648 and -2147483648 + +=item C + +Checks for an unsigned integer between 0 and 4294967295. + +=item C + +Checks for something that looks like a number. Scientic notation is not allowed. No range enforced. + +=back + =item C If validate_if is specified, the field will only be validated @@ -1079,19 +1171,36 @@ samples/validate_js_2_onchange.html to highlight the row and set an icon. = '!'; document.getElementById(args.key+'_row').style.background = '#ffdddd'; -}; + }; -document.validate_clear_hook = function (args) { - if (args.was_valid) { + document.validate_clear_hook = function (args) { + if (args.was_valid) { document.getElementById(args.key+'_img').innerHTML = '+'; document.getElementById(args.key+'_row').style.background = '#ddffdd'; - } else { + } else { document.getElementById(args.key+'_img').innerHTML = ''; document.getElementById(args.key+'_row').style.background = '#fff'; - } -}; + } + }; + +If you have jquery that looks like: + + document.validate_set_hook = function (args) { + $('#'+args.key+'_img').html('!'); + $('#'+args.key+'_row').css('backgroundColor', '#ffdddd'); + }; + + document.validate_clear_hook = function (args) { + if (args.was_valid) { + $('#'+args.key+'_img').html('+'); + $('#'+args.key+'_row').css('backgroundColor', '#ddffdd'); + } else { + $('#'+args.key+'_img').html(''); + $('#'+args.key+'_row').css('backgroundColor', '#fff'); + } + }; These hooks can also be set as "group clear_hook" and "group set_hook" which are defined further above. @@ -1104,11 +1213,6 @@ These hooks can also be set as "group clear_hook" and "group set_hook" that if the javascript didn't validate correctly, the user can still submit the data. -=head1 THANKS - -Thanks to Eamon Daly for providing bug fixes for bugs in validate.js -caused by HTML::Prototype. - =head1 LICENSE This module may be distributed under the same terms as Perl itself.