.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" 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 .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . 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 "Mojolicious::Validator::Validation 3pm" .TH Mojolicious::Validator::Validation 3pm 2024-05-15 "perl v5.38.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 Mojolicious::Validator::Validation \- Perform validations .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& use Mojolicious::Validator; \& use Mojolicious::Validator::Validation; \& \& my $validator = Mojolicious::Validator\->new; \& my $v = Mojolicious::Validator::Validation\->new(validator => $validator); \& $v\->input({foo => \*(Aqbar\*(Aq}); \& $v\->required(\*(Aqfoo\*(Aq)\->in(\*(Aqbar\*(Aq, \*(Aqbaz\*(Aq); \& say $v\->param(\*(Aqfoo\*(Aq); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Mojolicious::Validator::Validation performs Mojolicious::Validator validation checks. .SH ATTRIBUTES .IX Header "ATTRIBUTES" Mojolicious::Validator::Validation implements the following attributes. .SS csrf_token .IX Subsection "csrf_token" .Vb 2 \& my $token = $v\->csrf_token; \& $v = $v\->csrf_token(\*(Aqfa6a08...\*(Aq); .Ve .PP CSRF token. .SS input .IX Subsection "input" .Vb 2 \& my $input = $v\->input; \& $v = $v\->input({foo => \*(Aqbar\*(Aq, baz => [123, \*(Aqyada\*(Aq]}); .Ve .PP Data to be validated. .SS output .IX Subsection "output" .Vb 2 \& my $output = $v\->output; \& $v = $v\->output({foo => \*(Aqbar\*(Aq, baz => [123, \*(Aqyada\*(Aq]}); .Ve .PP Validated data. .SS topic .IX Subsection "topic" .Vb 2 \& my $topic = $v\->topic; \& $v = $v\->topic(\*(Aqfoo\*(Aq); .Ve .PP Name of field currently being validated. .SS validator .IX Subsection "validator" .Vb 2 \& my $v = $v\->validator; \& $v = $v\->validator(Mojolicious::Validator\->new); .Ve .PP Mojolicious::Validator object this validation belongs to. .SH METHODS .IX Header "METHODS" Mojolicious::Validator::Validation inherits all methods from Mojo::Base and implements the following new ones. .SS check .IX Subsection "check" .Vb 1 \& $v = $v\->check(\*(Aqsize\*(Aq, 2, 7); .Ve .PP Perform validation check on all values of the current "topic", no more checks will be performed on them after the first one failed. All checks from "CHECKS" in Mojolicious::Validator are supported. .SS csrf_protect .IX Subsection "csrf_protect" .Vb 1 \& $v = $v\->csrf_protect; .Ve .PP Validate \f(CW\*(C`csrf_token\*(C'\fR and protect from cross-site request forgery. .SS error .IX Subsection "error" .Vb 3 \& my $err = $v\->error(\*(Aqfoo\*(Aq); \& $v = $v\->error(foo => [\*(Aqcustom_check\*(Aq]); \& $v = $v\->error(foo => [$check, $result, @args]); .Ve .PP Get or set details for failed validation check, at any given time there can only be one per field. .PP .Vb 2 \& # Details about failed validation \& my ($check, $result, @args) = @{$v\->error(\*(Aqfoo\*(Aq)}; \& \& # Force validation to fail for a field without performing a check \& $v\->error(foo => [\*(Aqsome_made_up_check_name\*(Aq]); .Ve .SS every_param .IX Subsection "every_param" .Vb 2 \& my $values = $v\->every_param; \& my $values = $v\->every_param(\*(Aqfoo\*(Aq); .Ve .PP Similar to "param", but returns all values sharing the same name as an array reference. .PP .Vb 2 \& # Get first value \& my $first = $v\->every_param(\*(Aqfoo\*(Aq)\->[0]; .Ve .SS failed .IX Subsection "failed" .Vb 1 \& my $names = $v\->failed; .Ve .PP Return an array reference with all names for values that failed validation. .PP .Vb 2 \& # Names of all values that failed \& say for @{$v\->failed}; .Ve .SS has_data .IX Subsection "has_data" .Vb 1 \& my $bool = $v\->has_data; .Ve .PP Check if "input" is available for validation. .SS has_error .IX Subsection "has_error" .Vb 2 \& my $bool = $v\->has_error; \& my $bool = $v\->has_error(\*(Aqfoo\*(Aq); .Ve .PP Check if validation resulted in errors, defaults to checking all fields. .SS is_valid .IX Subsection "is_valid" .Vb 2 \& my $bool = $v\->is_valid; \& my $bool = $v\->is_valid(\*(Aqfoo\*(Aq); .Ve .PP Check if validation was successful and field has a value, defaults to checking the current "topic". .SS optional .IX Subsection "optional" .Vb 2 \& $v = $v\->optional(\*(Aqfoo\*(Aq); \& $v = $v\->optional(\*(Aqfoo\*(Aq, @filters); .Ve .PP Change validation "topic" and apply filters. All filters from "FILTERS" in Mojolicious::Validator are supported. .PP .Vb 2 \& # Trim value and check size \& $v\->optional(\*(Aquser\*(Aq, \*(Aqtrim\*(Aq)\->size(1, 15); .Ve .SS param .IX Subsection "param" .Vb 2 \& my $value = $v\->param; \& my $value = $v\->param(\*(Aqfoo\*(Aq); .Ve .PP Access validated values, defaults to the current "topic". If there are multiple values sharing the same name, and you want to access more than just the last one, you can use "every_param". .PP .Vb 2 \& # Get value right away \& my $user = $v\->optional(\*(Aquser\*(Aq)\->size(1, 15)\->param; .Ve .SS passed .IX Subsection "passed" .Vb 1 \& my $names = $v\->passed; .Ve .PP Return an array reference with all names for values that passed validation. .PP .Vb 2 \& # Names of all values that passed \& say for @{$v\->passed}; .Ve .SS required .IX Subsection "required" .Vb 2 \& $v = $v\->required(\*(Aqfoo\*(Aq); \& $v = $v\->required(\*(Aqfoo\*(Aq, @filters); .Ve .PP Change validation "topic", apply filters, and make sure a value is present. All filters from "FILTERS" in Mojolicious::Validator are supported. .PP .Vb 2 \& # Trim value and check size \& $v\->required(\*(Aquser\*(Aq, \*(Aqtrim\*(Aq)\->size(1, 15); .Ve .SH CHECKS .IX Header "CHECKS" In addition to the "ATTRIBUTES" and "METHODS" above, you can also call validation checks provided by "validator" on Mojolicious::Validator::Validation objects, similar to "check". .PP .Vb 4 \& # Call validation checks \& $v\->required(\*(Aqfoo\*(Aq)\->size(2, 5)\->like(qr/^[A\-Z]/); \& $v\->optional(\*(Aqbar\*(Aq)\->equal_to(\*(Aqfoo\*(Aq); \& $v\->optional(\*(Aqbaz\*(Aq)\->in(\*(Aqtest\*(Aq, \*(Aq123\*(Aq); \& \& # Longer version \& $v\->required(\*(Aqfoo\*(Aq)\->check(\*(Aqsize\*(Aq, 2, 5)\->check(\*(Aqlike\*(Aq, qr/^[A\-Z]/); .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" Mojolicious, Mojolicious::Guides, .