1Mojolicious::Validator(U3s)er Contributed Perl DocumentatMioojnolicious::Validator(3)
2
3
4
6 Mojolicious::Validator - Validate values
7
9 use Mojolicious::Validator;
10
11 my $validator = Mojolicious::Validator->new;
12 my $v = $validator->validation;
13 $v->input({foo => 'bar'});
14 $v->required('foo')->like(qr/ar$/);
15 say $v->param('foo');
16
18 Mojolicious::Validator validates values for Mojolicious.
19
21 These validation checks are available by default.
22
23 equal_to
24 $v = $v->equal_to('foo');
25
26 String value needs to be equal to the value of another field.
27
28 in
29 $v = $v->in('foo', 'bar', 'baz');
30
31 String value needs to match one of the values in the list.
32
33 like
34 $v = $v->like(qr/^[A-Z]/);
35
36 String value needs to match the regular expression.
37
38 num
39 $v = $v->num;
40 $v = $v->num(2, 5);
41 $v = $v->num(-3, 7);
42 $v = $v->num(2, undef);
43 $v = $v->num(undef, 5);
44
45 String value needs to be a non-fractional number (positive or negative)
46 and if provided in the given range.
47
48 size
49 $v = $v->size(2, 5);
50 $v = $v->size(2, undef);
51 $v = $v->size(undef, 5);
52
53 String value length or size of Mojo::Upload object in bytes needs to be
54 between these two values.
55
56 upload
57 $v = $v->upload;
58
59 Value needs to be a Mojo::Upload object, representing a file upload.
60
62 These filters are available by default.
63
64 not_empty
65 $v = $v->optional('foo', 'not_empty');
66
67 Remove empty string values and treat them as if they had not been
68 submitted.
69
70 trim
71 $v = $v->optional('foo', 'trim');
72
73 Trim whitespace characters from both ends of string value with "trim"
74 in Mojo::Util.
75
77 Mojolicious::Validator implements the following attributes.
78
79 checks
80 my $checks = $validator->checks;
81 $validator = $validator->checks({size => sub ($v, $name, $value, @args) {...}});
82
83 Registered validation checks, by default only "equal_to", "in", "like",
84 "num", "size" and "upload" are already defined.
85
86 filters
87 my $filters = $validator->filters;
88 $validator = $validator->filters({trim => sub {...}});
89
90 Registered filters, by default only "not_empty" and "trim" are already
91 defined.
92
94 Mojolicious::Validator inherits all methods from Mojo::Base and
95 implements the following new ones.
96
97 add_check
98 $validator = $validator->add_check(size => sub ($v, $name, $value, @args) {...});
99
100 Register a validation check.
101
102 $validator->add_check(foo => sub ($v, $name, $value, @args) {
103 ...
104 return undef;
105 });
106
107 add_filter
108 $validator = $validator->add_filter(trim => sub ($v, $name, $value) {...});
109
110 Register a new filter.
111
112 $validator->add_filter(foo => sub ($v, $name, $value) {
113 ...
114 return $value;
115 });
116
117 new
118 my $validator = Mojolicious::Validator->new;
119
120 Construct a new Mojolicious::Validator object.
121
122 validation
123 my $v = $validator->validation;
124
125 Build Mojolicious::Validator::Validation object to perform validations.
126
127 my $v = $validator->validation;
128 $v->input({foo => 'bar'});
129 $v->required('foo')->size(1, 5);
130 say $v->param('foo');
131
133 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
134
135
136
137perl v5.32.1 2021-02-07 Mojolicious::Validator(3)