1Mojolicious::Validator:U:sVearliCdoanttiroinb(u3t)ed PerMlojDoolciucmieonutsa:t:iVoanlidator::Validation(3)
2
3
4

NAME

6       Mojolicious::Validator::Validation - Perform validations
7

SYNOPSIS

9         use Mojolicious::Validator;
10         use Mojolicious::Validator::Validation;
11
12         my $validator = Mojolicious::Validator->new;
13         my $v = Mojolicious::Validator::Validation->new(validator => $validator);
14         $v->input({foo => 'bar'});
15         $v->required('foo')->in('bar', 'baz');
16         say $v->param('foo');
17

DESCRIPTION

19       Mojolicious::Validator::Validation performs Mojolicious::Validator
20       validation checks.
21

ATTRIBUTES

23       Mojolicious::Validator::Validation implements the following attributes.
24
25   csrf_token
26         my $token = $v->csrf_token;
27         $v        = $v->csrf_token('fa6a08...');
28
29       CSRF token.
30
31   input
32         my $input = $v->input;
33         $v        = $v->input({foo => 'bar', baz => [123, 'yada']});
34
35       Data to be validated.
36
37   output
38         my $output = $v->output;
39         $v         = $v->output({foo => 'bar', baz => [123, 'yada']});
40
41       Validated data.
42
43   topic
44         my $topic = $v->topic;
45         $v        = $v->topic('foo');
46
47       Name of field currently being validated.
48
49   validator
50         my $v = $v->validator;
51         $v    = $v->validator(Mojolicious::Validator->new);
52
53       Mojolicious::Validator object this validation belongs to.
54

METHODS

56       Mojolicious::Validator::Validation inherits all methods from Mojo::Base
57       and implements the following new ones.
58
59   check
60         $v = $v->check('size', 2, 7);
61
62       Perform validation check on all values of the current "topic", no more
63       checks will be performed on them after the first one failed. All checks
64       from "CHECKS" in Mojolicious::Validator are supported.
65
66   csrf_protect
67         $v = $v->csrf_protect;
68
69       Validate "csrf_token" and protect from cross-site request forgery.
70
71   error
72         my $err = $v->error('foo');
73         $v      = $v->error(foo => ['custom_check']);
74         $v      = $v->error(foo => [$check, $result, @args]);
75
76       Get or set details for failed validation check, at any given time there
77       can only be one per field.
78
79         # Details about failed validation
80         my ($check, $result, @args) = @{$v->error('foo')};
81
82         # Force validation to fail for a field without performing a check
83         $v->error(foo => ['some_made_up_check_name']);
84
85   every_param
86         my $values = $v->every_param;
87         my $values = $v->every_param('foo');
88
89       Similar to "param", but returns all values sharing the same name as an
90       array reference.
91
92         # Get first value
93         my $first = $v->every_param('foo')->[0];
94
95   failed
96         my $names = $v->failed;
97
98       Return an array reference with all names for values that failed
99       validation.
100
101         # Names of all values that failed
102         say for @{$v->failed};
103
104   has_data
105         my $bool = $v->has_data;
106
107       Check if "input" is available for validation.
108
109   has_error
110         my $bool = $v->has_error;
111         my $bool = $v->has_error('foo');
112
113       Check if validation resulted in errors, defaults to checking all
114       fields.
115
116   is_valid
117         my $bool = $v->is_valid;
118         my $bool = $v->is_valid('foo');
119
120       Check if validation was successful and field has a value, defaults to
121       checking the current "topic".
122
123   optional
124         $v = $v->optional('foo');
125         $v = $v->optional('foo', @filters);
126
127       Change validation "topic" and apply filters. All filters from "FILTERS"
128       in Mojolicious::Validator are supported.
129
130         # Trim value and check size
131         $v->optional('user', 'trim')->size(1, 15);
132
133   param
134         my $value = $v->param;
135         my $value = $v->param('foo');
136
137       Access validated values, defaults to the current "topic". If there are
138       multiple values sharing the same name, and you want to access more than
139       just the last one, you can use "every_param".
140
141         # Get value right away
142         my $user = $v->optional('user')->size(1, 15)->param;
143
144   passed
145         my $names = $v->passed;
146
147       Return an array reference with all names for values that passed
148       validation.
149
150         # Names of all values that passed
151         say for @{$v->passed};
152
153   required
154         $v = $v->required('foo');
155         $v = $v->required('foo', @filters);
156
157       Change validation "topic", apply filters, and make sure a value is
158       present. All filters from "FILTERS" in Mojolicious::Validator are
159       supported.
160
161         # Trim value and check size
162         $v->required('user', 'trim')->size(1, 15);
163

CHECKS

165       In addition to the "ATTRIBUTES" and "METHODS" above, you can also call
166       validation checks provided by "validator" on
167       Mojolicious::Validator::Validation objects, similar to "check".
168
169         # Call validation checks
170         $v->required('foo')->size(2, 5)->like(qr/^[A-Z]/);
171         $v->optional('bar')->equal_to('foo');
172         $v->optional('baz')->in('test', '123');
173
174         # Longer version
175         $v->required('foo')->check('size', 2, 5)->check('like', qr/^[A-Z]/);
176

SEE ALSO

178       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
179
180
181
182perl v5.34.0                      2021-07-M2o2jolicious::Validator::Validation(3)
Impressum