.\" Automatically generated by Pod::Man 4.10 (Pod::Simple 3.35) .\" .\" 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 "Mojolicious::Validator::Validation 3pm" .TH Mojolicious::Validator::Validation 3pm "2019-02-05" "perl v5.28.1" "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 \&\s-1CSRF\s0 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 \*(L"topic\*(R", no more checks will be performed on them after the first one failed. All checks from \&\*(L"\s-1CHECKS\*(R"\s0 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 \*(L"param\*(R", 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 \*(L"input\*(R" 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 \*(L"topic\*(R". .SS "optional" .IX Subsection "optional" .Vb 2 \& $v = $v\->optional(\*(Aqfoo\*(Aq); \& $v = $v\->optional(\*(Aqfoo\*(Aq, \*(Aqfilter1\*(Aq, \*(Aqfilter2\*(Aq); .Ve .PP Change validation \*(L"topic\*(R" and apply filters. All filters from \&\*(L"\s-1FILTERS\*(R"\s0 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 \*(L"topic\*(R". If there are multiple values sharing the same name, and you want to access more than just the last one, you can use \*(L"every_param\*(R". .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, \*(Aqfilter1\*(Aq, \*(Aqfilter2\*(Aq); .Ve .PP Change validation \*(L"topic\*(R", apply filters, and make sure a value is present and not an empty string. All filters from \*(L"\s-1FILTERS\*(R"\s0 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 \*(L"\s-1ATTRIBUTES\*(R"\s0 and \*(L"\s-1METHODS\*(R"\s0 above, you can also call validation checks provided by \*(L"validator\*(R" on Mojolicious::Validator::Validation objects, similar to \*(L"check\*(R". .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, .