.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.16) .\" .\" 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" '' '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 turned on, 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. .ie \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .el \{\ . de IX .. .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Validation::Class::Prototype 3pm" .TH Validation::Class::Prototype 3pm "2012-06-28" "perl v5.14.2" "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" Validation::Class::Prototype \- Prototype and Data Validation Engine for Validation::Class .SH "VERSION" .IX Header "VERSION" version 7.70 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& package MyApp::User; \& \& use Validation::Class; \& \& # import other class config, etc \& \& set { \& # ... \& }; \& \& # a mixin template \& \& mxn \*(Aqbasic\*(Aq => { \& required => 1 \& }; \& \& # a validation rule \& \& fld \*(Aqlogin\*(Aq => { \& label => \*(AqUser Login\*(Aq, \& error => \*(AqLogin invalid.\*(Aq, \& mixin => \*(Aqbasic\*(Aq, \& \& validation => sub { \& \& my ($self, $this_field, $all_params) = @_; \& \& return $this_field\->value eq \*(Aqadmin\*(Aq ? 1 : 0; \& \& } \& \& }; \& \& # a validation rule \& \& fld \*(Aqpassword\*(Aq => { \& label => \*(AqUser Password\*(Aq, \& error => \*(AqPassword invalid.\*(Aq, \& mixin => \*(Aqbasic\*(Aq, \& \& validation => sub { \& \& my ($self, $this_field, $all_params) = @_; \& \& return $this_field\->value eq \*(Aqpass\*(Aq ? 1 : 0; \& \& } \& \& }; \& \& # a validation profile \& \& pro \*(Aqregistration\*(Aq => sub { \& \& my ($self, @args) = @_; \& \& return $self\->validate(qw(+name +email \-login +password)) \& \& }; \& \& # an auto\-validating method \& \& mth \*(Aqregister\*(Aq => { \& \& input => \*(Aqregistration\*(Aq, \& using => sub { \& \& my ($self, @args) = shift; \& \& # ... do something \& \& } \& \& }; \& \& 1; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Validation::Class::Prototype provides the data validation schema and routines for all Validation::Class based classes. This class inherits from Validation::Class::Base. .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" .SS "directives" .IX Subsection "directives" The directives attribute returns a hashref of all defined directives. .PP .Vb 2 \& my $directives = $self\->directives(); \& ... .Ve .SS "errors" .IX Subsection "errors" The errors attribute provides error handling functionality and \s-1CANNOT\s0 be overridden. This attribute is a Validation::Class::Errors object. .PP .Vb 2 \& my $errors = $self\->errors(); \& ... .Ve .SS "fields" .IX Subsection "fields" The fields attribute provides field handling functionality and \s-1CANNOT\s0 be overridden. This attribute is a Validation::Class::Fields object. .PP .Vb 2 \& my $fields = $self\->fields(); \& ... .Ve .SS "filtering" .IX Subsection "filtering" The filtering attribute (by default set to 'pre') controls when incoming data is filtered. Setting this attribute to 'post' will defer filtering until after validation occurs which allows any errors messages to report errors based on the unaltered data. Alternatively, setting the filtering attribute to '' or undef will bypass all filtering unless explicitly defined at the field-level. .PP .Vb 1 \& my $filtering = $self\->filtering(\*(Aqpost\*(Aq); \& \& $self\->validate(); \& ... .Ve .SS "filters" .IX Subsection "filters" The filters attribute returns a hashref of pre-defined filter definitions. .PP .Vb 2 \& my $filters = $self\->filters(); \& ... .Ve .SS "hash_inflator" .IX Subsection "hash_inflator" The hash_inflator attribute determines how the hash serializer (inflation/deflation) behaves. The value must be a hashref of \*(L"\s-1OPTIONS\s0\*(R" in Hash::Flatten options. Purely for the sake of consistency, you can use lowercase keys (with underscores) which will be converted to camel-cased keys before passed to the serializer. .PP .Vb 4 \& my $self = MyApp::Validation\->new(hash_inflator => { \& hash_delimiter => \*(Aq/\*(Aq, \& array_delimiter => \*(Aq//\*(Aq \& }); \& \& ... \& \& # then, hereinafter referred to as \& \& $self\->hash_inflator\->{HashDelimiter} = \*(Aq/\*(Aq; \& $self\->hash_inflator\->{ArrayDelimiter} = \*(Aq//\*(Aq; .Ve .SS "ignore_failure" .IX Subsection "ignore_failure" The ignore_failure boolean determines whether your application will live or die upon failing to validate a self-validating method defined using the method keyword. This is on (1) by default, method validation failures will set errors and can be determined by checking the error stack using one of the error message methods. If turned off, the application will die and confess on failure. .PP .Vb 2 \& my $ignoring = $self\->ignore_failure(1); \& ... .Ve .SS "ignore_unknown" .IX Subsection "ignore_unknown" The ignore_unknown boolean determines whether your application will live or die upon encountering unregistered field directives during validation. This is off (0) by default, attempts to validate unknown fields \s-1WILL\s0 cause the program to die. .PP .Vb 2 \& my $ignoring = $self\->ignore_unknown(1); \& ... .Ve .SS "methods" .IX Subsection "methods" The methods attribute returns a hashref of self-validating method definitions. .PP .Vb 2 \& my $methods = $self\->methods(); # definitions are hashrefs \& ... .Ve .SS "mixins" .IX Subsection "mixins" The mixins attribute returns a hashref of defined validation templates. .PP .Vb 2 \& my $mixins = $self\->mixins(); \& ... .Ve .SS "params" .IX Subsection "params" The params attribute provides parameter handling functionality and \s-1CANNOT\s0 be overridden. This attribute is a Validation::Class::Params object. .PP .Vb 2 \& my $params = $self\->params(); \& ... .Ve .SS "plugins" .IX Subsection "plugins" The plugins attribute returns a hashref of loaded plugins. .PP .Vb 2 \& my $plugins = $self\->plugins(); \& ... .Ve .SS "profiles" .IX Subsection "profiles" The profiles attribute returns a hashref of validation profiles. .PP .Vb 2 \& my $profiles = $self\->profiles(); \& ... .Ve .SS "queued" .IX Subsection "queued" The queued attribute returns an arrayref of field names for (auto) validation. It represents a list of field names stored to be used in validation later. If the queued attribute contains a list, you can omit arguments to the validate method. .PP .Vb 2 \& my $queued = $self\->queued([qw/.../]); \& ... .Ve .SS "relatives" .IX Subsection "relatives" The relatives attribute returns a hashref of short\-name/class\-name pairs of loaded child classes. .PP .Vb 2 \& my $relatives = $self\->relatives(); \& ... .Ve .SS "report_failure" .IX Subsection "report_failure" The report_failure boolean determines whether your application will report self-validating method failures as class-level errors. This is off (0) by default, if turned on, an error messages will be generated and set at the class-level specifying the method which failed in addition to the existing messages. .PP .Vb 2 \& my $reporting = $self\->report_failure(0); \& ... .Ve .SS "report_unknown" .IX Subsection "report_unknown" The report_unknown boolean determines whether your application will report unregistered fields as class-level errors upon encountering unregistered field directives during validation. This is off (0) by default, attempts to validate unknown fields will \s-1NOT\s0 be registered as class-level variables. .PP .Vb 2 \& my $reporting = $self\->report_unknown(1); \& ... .Ve .SH "METHODS" .IX Header "METHODS" .SS "apply_filters" .IX Subsection "apply_filters" The apply_filters method (usually called automatically based on the filtering attribute) can be used to run the currently defined parameters through the filters defined in their matching fields. .PP .Vb 1 \& $self = $self\->apply_filters; \& \& # apply filters to fields labeled as "don\*(Aqt filter automatically" (post) \& $self = $self\->apply_filters(\*(Aqpost\*(Aq); .Ve .SS "class" .IX Subsection "class" The class method returns a new initialize validation class related to the namespace of the calling class, the relative class would've been loaded via the \&\*(L"load\*(R" keyword. .PP Existing parameters and configuration options are passed to the relative class' constructor (including the stash). All attributes can be easily overwritten using the attribute's accessors on the relative class. .PP Also, you may prevent/override arguments from being copied to the new class object by supplying the them as arguments to this method. .PP The class method is also quite handy in that it will detect parameters that are prefixed with the name of the class being fetched, and adjust the matching rule (if any) to allow validation to occur. .PP .Vb 1 \& package Class; \& \& use Validation::Class; \& \& load classes => 1; # load child classes e.g. Class::* \& \& package main; \& \& my $input = Class\->new(params => $params); \& \& my $child1 = $input\->class(\*(AqChild\*(Aq); # loads Class::Child; \& my $child2 = $input\->class(\*(AqStepChild\*(Aq); # loads Class::StepChild; \& \& my $child3 = $input\->class(\*(Aqchild\*(Aq); # loads Class::Child; \& my $child4 = $input\->class(\*(Aqstep_child\*(Aq); # loads Class::StepChild; \& \& # use any non\-alphanumeric character or underscore as the namespace delimiter \& \& my $child5 = $input\->class(\*(Aqstep/child\*(Aq); # loads Class::Step::Child; \& my $child5a = $input\->class(\*(Aqstep:child\*(Aq); # loads Class::Step::Child; \& my $child5b = $input\->class(\*(Aqstep.child\*(Aq); # loads Class::Step::Child; \& my $child5c = $input\->class(\*(Aqstep\-child\*(Aq); # loads Class::Step::Child; \& \& my $child6 = $input\->class(\*(AqCHILD\*(Aq); # loads Class::CHILD; \& \& # intelligently detecting and map params to child class \& \& my $params = { \& \& \*(Aqmy.name\*(Aq => \*(AqGuy Friday\*(Aq, \& \*(Aqchild.name\*(Aq => \*(AqGuy Friday Jr.\*(Aq \& \& }; \& \& $input\->class(\*(Aqchild\*(Aq); # child field *name* mapped to param *child.name* \& \& # without copying params from class \& \& my $child = $input\->class(\*(Aqchild\*(Aq, params => {}); \& \& # alternate syntax \& \& my $child = $input\->class(name => \*(Aqchild\*(Aq, params => {}); \& \& 1; .Ve .SS "clear_queue" .IX Subsection "clear_queue" The clear_queue method resets the queue container, see the queue method for more information on queuing fields to be validated. The clear_queue method has yet another useful behavior in that it can assign the values of the queued parameters to the list it is passed, where the values are assigned in the same order queued. .PP .Vb 1 \& my $self = Class\->new(params => $params); \& \& $self\->queue(qw(name +email)); \& \& ... \& \& $self\->queue(qw(+login +password)); \& \& if ($self\->validate) { \& \& $self\->clear_queue(my($name, $email)); \& \& print "Name is $name"; \& \& } .Ve .SS "clone" .IX Subsection "clone" The clone method is used to create new fields (rules) from existing fields on-the-fly. This is useful when you have a variable number of parameters being validated that can share existing validation rules. .PP .Vb 1 \& package Class; \& \& use Validation::Class; \& \& field \*(Aqphone\*(Aq => { \& label => \*(AqYour Phone\*(Aq, \& required => 1 \& }; \& \& package main; \& \& my $self = Class\->new(params => $params); \& \& # clone phone rule at run\-time to validate dynamically created parameters \& $self\->clone(\*(Aqphone\*(Aq, \*(Aqphone2\*(Aq, { label => \*(AqOther Phone\*(Aq, required => 0 }); \& $self\->clone(\*(Aqphone\*(Aq, \*(Aqphone3\*(Aq, { label => \*(AqThird Phone\*(Aq, required => 0 }); \& $self\->clone(\*(Aqphone\*(Aq, \*(Aqphone4\*(Aq, { label => \*(AqForth Phone\*(Aq, required => 0 }); \& \& $self\->validate(qw/phone phone2 phone3 phone4/); \& \& 1; .Ve .SS "error_count" .IX Subsection "error_count" The error_count method returns the total number of errors set at both the class and field level. .PP .Vb 1 \& my $count = $self\->error_count; .Ve .SS "error_fields" .IX Subsection "error_fields" The error_fields method returns a hashref containing the names of fields which failed validation and an arrayref of error messages. .PP .Vb 1 \& unless ($self\->validate) { \& \& my $failed = $self\->error_fields; \& \& } \& \& my $suspects = $self\->error_fields(\*(Aqfield2\*(Aq, \*(Aqfield3\*(Aq); .Ve .SS "errors_to_string" .IX Subsection "errors_to_string" The errors_to_string method stringifies the all error objects on both the class and fields using the specified delimiter (defaulting to comma-space (\*(L", \*(R")). .PP .Vb 2 \& return $self\->errors_to_string("\en"); \& return $self\->errors_to_string(undef, sub{ ucfirst lc shift }); \& \& unless ($self\->validate) { \& \& return $self\->errors_to_string; \& \& } .Ve .SS "flatten_params" .IX Subsection "flatten_params" The flatten_params method accepts a single hashref and will return the serialized version based on the the default or custom configuration of the hash serializer Hash::Flatten as configured via the \fIhash_inflator()\fR attribute. .PP .Vb 1 \& my $params = { \& \& user => { \& login => \*(Aqmember\*(Aq, \& password => \*(Aqabc123456\*(Aq \& } \& \& }; \& \& my $serialized_params = $self\->flatten_params($params); .Ve .SS "get_errors" .IX Subsection "get_errors" The get_errors method returns a list of combined class-and-field-level errors. .PP .Vb 1 \& my @errors = $self\->get_errors; # returns list \& \& my @critical = $self\->get_errors(qr/^critical:/i); # filter errors \& \& my @specific_field_errors = $self\->get_errors(\*(Aqfield_a\*(Aq, \*(Aqfield_b\*(Aq); .Ve .SS "get_fields" .IX Subsection "get_fields" The get_fields method returns the list of references to the specified fields. Returns undef if no arguments are passed. This method is likely to be used more internally than externally. .PP .Vb 1 \& my ($this, $that) = $self\->get_fields(\*(Aqthis\*(Aq, \*(Aqthat\*(Aq); .Ve .SS "get_params" .IX Subsection "get_params" The get_params method returns the values of the parameters specified (as a list, in the order specified). This method will return a list of key/value pairs if no parameter names are passed. Note, this method always returns exploded/unflattened parameters. .PP .Vb 1 \& if ($self\->validate) { \& \& my ($name) = $self\->get_params(\*(Aqname\*(Aq); \& \& my ($name, $email, $login, $password) = \& $self\->get_params(qw/name email login password/); \& \& # you should note that if the params don\*(Aqt exist they will return undef \& # ... meaning you should check that it is defined before doing any \& # comparison checking \-\- as doing so would generate an error \& # e.g. \& \& if (defined $name) { \& \& if ($name eq \*(Aq\*(Aq) { \& \& print \*(Aqname parameter was passed but was empty\*(Aq; \& \& } \& \& } \& \& else { \& \& print \*(Aqname parameter was never submitted\*(Aq; \& \& } \& \& } \& \& # alternatively ... \& \& my $params = $self\->get_params; # return hashref of parameters \& \& print $params\->{name}; .Ve .SS "get_value" .IX Subsection "get_value" The get_value method returns the absolute value (hardcoded, default or parameter specified) for a given field. This method executes specific logic which returns the value a field has based on a set of internal conditions. This method otherwise returns undefined. .PP .Vb 1 \& my $value = $self\->get_value(\*(Aqfield_name\*(Aq); .Ve .SS "normalize" .IX Subsection "normalize" The normalize method executes a set of routines that reset the parameter environment filtering any parameters present. This method is executed automatically at instantiation and again just before each validation event. .PP .Vb 1 \& $self\->normalize(); .Ve .SS "param" .IX Subsection "param" The param method gets/sets a single parameter by name. This method returns the value assigned or undefined if the parameter does not exist. .PP .Vb 1 \& my $value = $self\->param(\*(Aqname\*(Aq); \& \& $self\->param($name => $value); .Ve .SS "plugin" .IX Subsection "plugin" The plugin method returns the instantiated plugin object attached to the current class. .PP .Vb 1 \& package Class; \& \& use Validation::Class; \& \& load plugin => [\*(AqTelephoneFormat\*(Aq]; \& load plugin => [\*(Aq+Class::Plugin::Form::Elements\*(Aq]; \& \& package main; \& \& my $input = Class\->new(params => $params); \& \& # get object for Validation::Class::Plugin::TelephoneFormat; \& \& my $plugin = $input\->plugin(\*(Aqtelephone_format\*(Aq); \& \& # use any non\-alphanumeric character or underscore as the namespace delimiter \& # get object for Class::Plugin::Form::Elements; \& # note the leading character/delimiter \& \& # automatically resolves to the first matching namespace for $self \& # or Validation::Class::Plugin \& \& my $plugin = $input\->plugin(\*(Aqplugin:form:elements\*(Aq); \& \& # prefix with a special character to denote a fully\-qualified namespace \& \& my $plugin = $input\->plugin(\*(Aq+class:plugin:form:elements\*(Aq); \& \& # same as $input\->proto\->plugins\->{\*(AqClass::Plugin::Form::Elements\*(Aq}; \& \& my $plugin = $input\->plugin(\*(AqClass::Plugin::Form::Elements\*(Aq); .Ve .SS "queue" .IX Subsection "queue" The queue method is a convenience method used specifically to append the queued attribute allowing you to *queue* fields to be validated. This method also allows you to set fields that must always be validated. .PP .Vb 3 \& $self\->queue(qw/name login/); \& $self\->queue(qw/email_confirm/) if $input\->param(\*(Aqchg_email\*(Aq); \& $self\->queue(qw/password_confirm/) if $input\->param(\*(Aqchg_pass\*(Aq); .Ve .SS "reset" .IX Subsection "reset" The reset method clears all errors, fields and queued field names, both at the class and individual field levels. .PP .Vb 1 \& $self\->reset(); .Ve .SS "reset_errors" .IX Subsection "reset_errors" The reset_errors method clears all errors, both at the class and individual field levels. This method is called automatically every time the \fIvalidate()\fR method is triggered. .PP .Vb 1 \& $self\->reset_errors(); .Ve .SS "reset_fields" .IX Subsection "reset_fields" The reset_fields method clears all errors and field values, both at the class and individual field levels. This method is executed automatically at instantiation. .PP .Vb 1 \& $self\->reset_fields(); .Ve .SS "reset_params" .IX Subsection "reset_params" The reset_params method is responsible for completely removing any existing parameters and adding those specified. This method returns the class object. This method takes a list of key/value pairs or a single hashref. .PP .Vb 1 \& $self = $self\->reset_params($new_params); # accepts list also .Ve .SS "set_errors" .IX Subsection "set_errors" The set_errors method pushes its arguments (error messages) onto the class-level error stack and returns a count of class-level errors. .PP .Vb 1 \& my $count = $self\->set_errors(..., ...); .Ve .SS "set_fields" .IX Subsection "set_fields" The set_fields method is responsible setting/overriding registered fields. This method returns the class object. This method takes a list of key/value pairs or a single hashref whose key should be a valid field name and whose value should be a hashref that is a valid field configuration object. .PP .Vb 1 \& $self = $self\->set_fields($name => $config); # accepts hashref also .Ve .SS "set_method" .IX Subsection "set_method" The set_method method conveniently creates a method on the calling class, this method is primarily intended to be used during instantiation of a plugin during instantiation of the validation class. .PP .Vb 1 \& my $sub = $self\->set_method(do_something => sub { ... }); .Ve .PP Additionally, method names are flattened, e.g. ThisFunction will be converted to this_function for convenience and consistency. .SS "set_params" .IX Subsection "set_params" The set_params method is responsible for setting/replacing parameters while ensuring that previously serialized parameters are taken into account. This method returns the class object. This method takes a list of key/value pairs or a single hashref whose key should match a field's name and whose value should be a scalar or arrayref. .PP .Vb 1 \& $self = $self\->set_params($name => $value); # accepts hashref also .Ve .SS "set_value" .IX Subsection "set_value" The set_value method assigns a value to the specified field's parameter unless the field is readonly. .PP .Vb 1 \& $self = $self\->set_value($name => $value); .Ve .SS "stash" .IX Subsection "stash" The stash method provides a container for context/instance specific information. The stash is particularly useful when custom validation routines require insight into context/instance specific operations. .PP .Vb 1 \& package MyApp::Validation; \& \& use Validation::Class; \& \& fld \*(Aqemail\*(Aq => { \& \& validation => sub { \& \& my ($self) = @_; \& \& my $db = $self\->stash(\*(Aqdatabase\*(Aq); \& \& return $db\->find(...) ? 0 : 1 ; # email exists \& \& } \& \& }; \& \& package main; \& \& $self\->stash( { database => $dbix_object } ); \& $self\->stash( database => $dbix_object ); \& \& ... .Ve .SS "unflatten_params" .IX Subsection "unflatten_params" The unflatten_params method accepts a single *flat* hashref and will return the expanded version based on the the default or custom configuration of the hash serializer Hash::Flatten as configured via the \fIhash_inflator()\fR attribute. .PP .Vb 4 \& my $params = { \& \*(Aquser.login\*(Aq => \*(Aqmember\*(Aq, \& \*(Aquser.password\*(Aq => \*(Aqabc123456\*(Aq \& }; \& \& $params = $self\->unflatten_params($params); \& \& print $params\->{user}\->{login}; .Ve .SS "validate" .IX Subsection "validate" The validate method returns true/false depending on whether all specified fields passed validation checks. .PP .Vb 1 \& use MyApp::Validation; \& \& my $input = MyApp::Validation\->new(params => $params); \& \& # validate specific fields \& unless ($input\->validate(\*(Aqfield1\*(Aq,\*(Aqfield2\*(Aq)){ \& return $input\->errors_to_string; \& } \& \& # validate fields based on a regex pattern \& unless ($input\->validate(qr/^field(\ed+)?/)){ \& return $input\->errors_to_string; \& } \& \& # validate existing parameters, if no parameters exist, \& # validate all fields ... which will return true unless field(s) exist \& # with a required directive \& unless ($input\->validate()){ \& return $input\->errors_to_string; \& } \& \& # validate all fields period, obviously \& unless ($input\->validate(keys %{$input\->fields})){ \& return $input\->errors_to_string; \& } \& \& # validate specific parameters (by name) after mapping them to other fields \& my $parameter_map = { \& user => \*(Aqhey_im_not_named_login\*(Aq, \& pass => \*(Aqpassword_is_that_really_you\*(Aq \& }; \& unless ($input\->validate($parameter_map)){ \& return $input\->errors_to_string; \& } .Ve .PP Another cool trick the \fIvalidate()\fR method can perform is the ability to temporarily alter whether a field is required or not during run-time. This functionality is often referred to as the *toggle* function. .PP This method is important when you define a field (or two or three) as required or non and want to change that per validation. This is done by calling the \&\fIvalidate()\fR method with a list of fields to be validated and prefixing the target fields with a plus or minus as follows: .PP .Vb 1 \& use MyApp::Validation; \& \& my $input = MyApp::Validation\->new(params => $params); \& \& # validate specific fields, force name, email and phone to be required \& # regardless of the field definitions directives ... and force the age, sex \& # and birthday to be optional \& \& my @spec = qw(+name +email +phone \-age \-sex \-birthday); \& \& unless ($input\->validate(@spec)){ \& return $input\->errors_to_string; \& } .Ve .SS "validate_profile" .IX Subsection "validate_profile" The validate_profile method executes a stored validation profile, it requires a profile name and can be passed additional parameters which get forwarded into the profile routine in the order received. .PP .Vb 1 \& unless ($self\->validate_profile(\*(Aqpassword_change\*(Aq)) { \& \& print $self\->errors_to_string; \& \& } \& \& unless ($self\->validate_profile(\*(Aqemail_change\*(Aq, $dbi_handle)) { \& \& print $self\->errors_to_string; \& \& } .Ve .SH "DIRECTIVES" .IX Header "DIRECTIVES" .Vb 1 \& package MyApp::Validation; \& \& use Validation::Class; \& \& # a validation template \& \& mixin \*(Aq...\*(Aq => { \& # mixin directives here \& ... \& }; \& \& # a validation rule \& \& field \*(Aq...\*(Aq => { \& # field directives here \& ... \& }; \& \& 1; .Ve .PP When building a validation class, the first encountered and arguably two most important keyword functions are \fIfield()\fR and \fImixin()\fR, which are used to declare their respective properties. A \fImixin()\fR declares a validation template where its properties are intended to be copied within \fIfield()\fR declarations which declares validation rules, filters and other properties. .PP Both the \fIfield()\fR and \fImixin()\fR declarations/functions require two parameters, the first being a name, used to identify the declaration and to be matched against incoming input parameters and the second being a hashref of key/value pairs. The key(s) within a declaration are commonly referred to as directives. .PP The following is a list of default directives which can be used in field/mixin declarations: .SS "alias" .IX Subsection "alias" The alias directive is useful when many different parameters with different names can be validated using a single rule. E.g. The paging parameters in a webapp may take on different names but require the same validation. .PP .Vb 5 \& # the alias directive \& field \*(Aqpager\*(Aq => { \& alias => [\*(Aqpage_user_list\*(Aq, \*(Aqpage_other_list\*(Aq] \& ... \& }; .Ve .SS "default" .IX Subsection "default" The default directive is used as a default value for a field to be used when a matching parameter is not present. Optionally, the default directive can be a coderef allowing the default value to be set on request. .PP .Vb 5 \& # the default directive \& field \*(Aqquantity\*(Aq => { \& default => 1, \& ... \& }; \& \& # the default directive as a coderef \& field \*(Aqquantity\*(Aq => { \& default => sub { \& return DB::Settings\->default_quantity \& } \& }; .Ve .SS "error/errors" .IX Subsection "error/errors" The error/errors directive is used to replace the system generated error messages when a particular field doesn't validate. If a field fails multiple directives, multiple errors will be generated for the same field. This may not be desirable, the error directive overrides this behavior and only the specified error is registered and displayed. .PP .Vb 5 \& # the error(s) directive \& field \*(Aqfoobar\*(Aq => { \& errors => \*(AqFoobar failed processing, Wtf?\*(Aq, \& ... \& }; .Ve .SS "filtering" .IX Subsection "filtering" The filtering directive is used to control when field filters are applied. The default recognized values are pre/post. A value of 'pre' instructs the validation class to apply the field's filters at instantiation and before validation whereas a value of 'post' instructs the validation class to apply the field's filters after validation. Alternatively, a value of undef or '' will bypass filtering altogether. .PP .Vb 5 \& # the filtering directive \& field \*(Aqfoobar\*(Aq => { \& filtering => \*(Aqpost\*(Aq, \& ... \& }; .Ve .SS "label" .IX Subsection "label" The label directive is used as a user-friendly reference when the field name is a serialized hash key or just plain ugly. .PP .Vb 5 \& # the label directive \& field \*(Aqhashref.foo.bar\*(Aq => { \& label => \*(AqFoo Bar\*(Aq, \& ... \& }; .Ve .SS "mixin" .IX Subsection "mixin" The mixin directive is used to create a template of directives to be applied to other fields. .PP .Vb 5 \& mixin \*(AqID\*(Aq => { \& required => 1, \& min_length => 1, \& max_length => 11 \& }; \& \& # the mixin directive \& field \*(Aquser.id\*(Aq => { \& mixin => \*(AqID\*(Aq, \& ... \& }; .Ve .SS "mixin_field" .IX Subsection "mixin_field" The mixin directive is used to copy all directives from an existing field except for the name, label, and validation directives. .PP .Vb 5 \& # the mixin_field directive \& field \*(Aqfoobar\*(Aq => { \& label => \*(AqFoo Bar\*(Aq, \& required => 1 \& }; \& \& field \*(Aqbarbaz\*(Aq => { \& mixin_field => \*(Aqfoobar\*(Aq, \& label => \*(AqBar Baz\*(Aq, \& ... \& }; .Ve .SS "name" .IX Subsection "name" The name directive is used *internally* and cannot be changed. .PP .Vb 4 \& # the name directive \& field \*(Aqthename\*(Aq => { \& ... \& }; .Ve .SS "readonly" .IX Subsection "readonly" The readonly directive is used to symbolize a field whose parameter value should not be honored and if encountered, deleted. Unlike the read-only attribute options in other object systems, setting this will not cause you program to die and in-fact, an experience programmer can selectively bypass this constraint. .PP .Vb 5 \& # the readonly directive \& field \*(Aqthename\*(Aq => { \& readonly => 1, \& ... \& }; .Ve .SS "value" .IX Subsection "value" The value directive is used internally to store the field's matching parameter's value. This value can be set in the definition but \s-1SHOULD\s0 \s-1NOT\s0 \s-1BE\s0 used as a default value unless you're sure no parameter will overwrite it during run-time. If you need to set a default value, see the default directive. .PP .Vb 5 \& # the value directive \& field \*(Aqquantity\*(Aq => { \& value => 1, \& ... \& }; .Ve .SH "DIRECTIVES (FILTERS)" .IX Header "DIRECTIVES (FILTERS)" The filters directive is used to correct, alter and/or format the values of the matching input parameter. Note: Filtering is applied before validation. The filter directive can have multiple filters (even a coderef) in the form of an arrayref of values. .PP .Vb 7 \& # the filter(s) directive \& field \*(Aqtext\*(Aq => { \& filters => [qw/trim strip/ => sub { \& $_[0] =~ s/\eD//g; \& }], \& ... \& }; .Ve .PP The following is a list of default filters that may be used with the filter directive: .SS "alpha" .IX Subsection "alpha" The alpha filter removes all non-Alphabetic characters from the field's value. .PP .Vb 3 \& field \*(Aqfoobar\*(Aq => { \& filters => \*(Aqalpha\*(Aq, \& }; .Ve .SS "alphanumeric" .IX Subsection "alphanumeric" The alpha filter removes all non-Alphabetic and non-Numeric characters from the field's value. .PP .Vb 3 \& field \*(Aqfoobar\*(Aq => { \& filters => \*(Aqalphanumeric\*(Aq, \& }; .Ve .SS "capitalize" .IX Subsection "capitalize" The capitalize filter attempts to capitalize the first word in each sentence, where sentences are separated by a period and space, within the field's value. .PP .Vb 3 \& field \*(Aqfoobar\*(Aq => { \& filters => \*(Aqcapitalize\*(Aq, \& }; .Ve .SS "decimal" .IX Subsection "decimal" The decimal filter removes all non-decimal-based characters from the field's value. Allows only: decimal, comma, and numbers. .PP .Vb 3 \& field \*(Aqfoobar\*(Aq => { \& filters => \*(Aqdecimal\*(Aq, \& }; .Ve .SS "lowercase" .IX Subsection "lowercase" The lowercase filter converts the field's value to lowercase. .PP .Vb 3 \& field \*(Aqfoobar\*(Aq => { \& filters => \*(Aqlowercase\*(Aq, \& }; .Ve .SS "numeric" .IX Subsection "numeric" The numeric filter removes all non-numeric characters from the field's value. .PP .Vb 3 \& field \*(Aqfoobar\*(Aq => { \& filters => \*(Aqnumeric\*(Aq, \& }; .Ve .SS "strip" .IX Subsection "strip" As with the trim filter the strip filter removes leading and trailing whitespaces from the field's value and additionally removes multiple whitespaces from between the values characters. .PP .Vb 3 \& field \*(Aqfoobar\*(Aq => { \& filters => \*(Aqstrip\*(Aq, \& }; .Ve .SS "titlecase" .IX Subsection "titlecase" The titlecase filter converts the field's value to titlecase by capitalizing the first letter of each word. .PP .Vb 3 \& field \*(Aqfoobar\*(Aq => { \& filters => \*(Aqtitlecase\*(Aq, \& }; .Ve .SS "trim" .IX Subsection "trim" The trim filter removes leading and trailing whitespace from the field's value. .PP .Vb 3 \& field \*(Aqfoobar\*(Aq => { \& filters => \*(Aqtrim\*(Aq, \& }; .Ve .SS "uppercase" .IX Subsection "uppercase" The uppercase filter converts the field's value to uppercase. .PP .Vb 3 \& field \*(Aqfoobar\*(Aq => { \& filters => \*(Aquppercase\*(Aq, \& }; .Ve .SH "DIRECTIVES (VALIDATORS)" .IX Header "DIRECTIVES (VALIDATORS)" .Vb 1 \& package MyApp::Validation; \& \& use Validation::Class; \& \& # a validation rule with validator directives \& field \*(Aqtelephone_number\*(Aq => { \& length => 14, \& pattern => \*(Aq(###) ###\-####\*(Aq, \& ... \& }; \& \& 1; .Ve .PP Validator directives are special directives with associated validation code that is used to validate common use cases such as \*(L"checking the length of a parameter\*(R", etc. .PP Please note that a directive's value can be either a scalar or arrayref, most directives take a scalar value though some have the ability to take an arrayref or parse a delimited list. There is no official documentation as to which directives can accept lists of values although it will typically be evident. .PP .Vb 1 \& E.g. (a directive with a list of values) \& \& { \& directive => [qw(1 2 3)], \& directive => [\*(Aq1\*(Aq, \*(Aq2\*(Aq, \*(Aq3\*(Aq], \& directive => \*(Aq1, 2, 3\*(Aq, \& directive => \*(Aq1\-2\-3\*(Aq, \& directive => \*(Aq1,2,3\*(Aq, \& } .Ve .PP The following is a list of the default validators which can be used in field/mixin declarations: .SS "between" .IX Subsection "between" .Vb 5 \& # the between directive \& field \*(Aqfoobar\*(Aq => { \& between => \*(Aq1\-5\*(Aq, \& ... \& }; .Ve .SS "depends_on" .IX Subsection "depends_on" .Vb 5 \& # the depends_on directive \& field \*(Aqchange_password\*(Aq => { \& depends_on => [\*(Aqpassword\*(Aq, \*(Aqpassword_confirm\*(Aq], \& ... \& }; .Ve .SS "length" .IX Subsection "length" .Vb 5 \& # the length directive \& field \*(Aqfoobar\*(Aq => { \& length => 20, \& ... \& }; .Ve .SS "matches" .IX Subsection "matches" .Vb 5 \& # the matches directive \& field \*(Aqthis_field\*(Aq => { \& matches => \*(Aqanother_field\*(Aq, \& ... \& }; .Ve .SS "max_alpha" .IX Subsection "max_alpha" .Vb 5 \& # the max_alpha directive \& field \*(Aqpassword\*(Aq => { \& max_alpha => 30, \& ... \& }; .Ve .SS "max_digits" .IX Subsection "max_digits" .Vb 5 \& # the max_digits directive \& field \*(Aqpassword\*(Aq => { \& max_digits => 5, \& ... \& }; .Ve .SS "max_length" .IX Subsection "max_length" .Vb 5 \& # the max_length directive \& field \*(Aqfoobar\*(Aq => { \& max_length => \*(Aq...\*(Aq, \& ... \& }; .Ve .SS "max_sum" .IX Subsection "max_sum" .Vb 5 \& # the max_sum directive \& field \*(Aqvacation_days\*(Aq => { \& max_sum => 5, \& ... \& }; .Ve .SS "max_symbols" .IX Subsection "max_symbols" .Vb 5 \& # the max_symbols directive \& field \*(Aqpassword\*(Aq => { \& max_symbols => 1, \& ... \& }; .Ve .SS "min_alpha" .IX Subsection "min_alpha" .Vb 5 \& # the min_alpha directive \& field \*(Aqpassword\*(Aq => { \& min_alpha => 2, \& ... \& }; .Ve .SS "min_digits" .IX Subsection "min_digits" .Vb 5 \& # the min_digits directive \& field \*(Aqpassword\*(Aq => { \& min_digits => 1, \& ... \& }; .Ve .SS "min_length" .IX Subsection "min_length" .Vb 5 \& # the min_length directive \& field \*(Aqfoobar\*(Aq => { \& min_length => \*(Aq...\*(Aq, \& ... \& }; .Ve .SS "min_sum" .IX Subsection "min_sum" .Vb 5 \& # the min_sum directive \& field \*(Aqvacation_days\*(Aq => { \& min_sum => 0, \& ... \& }; .Ve .SS "min_symbols" .IX Subsection "min_symbols" .Vb 5 \& # the min_symbols directive \& field \*(Aqpassword\*(Aq => { \& min_symbols => 0, \& ... \& }; .Ve .SS "options" .IX Subsection "options" .Vb 5 \& # the options directive \& field \*(Aqstatus\*(Aq => { \& options => \*(AqActive, Inactive\*(Aq, \& ... \& }; .Ve .SS "pattern" .IX Subsection "pattern" .Vb 6 \& # the pattern directive \& field \*(Aqtelephone\*(Aq => { \& # simple pattern \& pattern => \*(Aq### ###\-####\*(Aq, \& ... \& }; \& \& field \*(Aqcountry_code\*(Aq => { \& # simple pattern \& pattern => \*(AqXX\*(Aq, \& filter => \*(Aquppercase\*(Aq \& ... \& }; \& \& field \*(Aqcomplex\*(Aq => { \& # regex pattern \& pattern => qr/[0\-9]+\e,\es\e.\e.\e./, \& ... \& }; .Ve .SS "required" .IX Subsection "required" The required directive is an important directive but can be misunderstood. The required directive is used to ensure the submitted parameter exists and has a value. If the parameter is never submitted, the directive is effectively skipped. This directive can be thought of as the \*(L"must-have-a-value-if-exists\*(R" directive. .PP .Vb 5 \& # the required directive \& field \*(Aqfoobar\*(Aq => { \& required => 1, \& ... \& }; \& \& # pass \& my $rules = MyApp::Validation\->new(params => { }); \& $rules\->validate(); #validate everything \& \& # fail \& my $rules = MyApp::Validation\->new(params => { foobar => \*(Aq\*(Aq }); \& $rules\->validate(); #validate everything \& \& # fail \& my $rules = MyApp::Validation\->new(params => { }); \& $rules\->validate(\*(Aqfoobar\*(Aq); \& \& # fail \& my $rules = MyApp::Validation\->new(params => { foobar => \*(Aq\*(Aq }); \& $rules\->validate(\*(Aqfoobar\*(Aq); \& \& # pass \& my $rules = MyApp::Validation\->new(params => { foobar => \*(AqNice\*(Aq }); \& $rules\->validate(\*(Aqfoobar\*(Aq); .Ve .PP See the toggle functionality within the \fIvalidate()\fR method. This method allows you to temporarily alter whether a field is required or not. .SS "validation" .IX Subsection "validation" The validation directive is a coderef used add additional custom validation to the field. The coderef must return true (to pass) or false (to fail). Custom error messages can be set from within the coderef so make sure they are set based on appropriate logic as the registration of error message are not contingent on the success or failure of the routine. .PP .Vb 2 \& # the validation directive \& field \*(Aqlogin\*(Aq => { \& \& validation => sub { \& \& my ($self, $this_field, $all_params) = @_; \& \& return 0 unless $this_field\->value; \& return $this_field\->value eq \*(Aqadmin\*(Aq ? 1 : 0; \& \& }, \& \& }; .Ve .SH "AUTHOR" .IX Header "AUTHOR" Al Newkirk .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2011 by Al Newkirk. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.