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 comma_separated
65 $v = $v->optional('foo', 'comma_separated');
66
67 Split string of comma separated values into separate values.
68
69 not_empty
70 $v = $v->optional('foo', 'not_empty');
71
72 Remove empty string values and treat them as if they had not been
73 submitted.
74
75 trim
76 $v = $v->optional('foo', 'trim');
77
78 Trim whitespace characters from both ends of string value with "trim"
79 in Mojo::Util.
80
82 Mojolicious::Validator implements the following attributes.
83
84 checks
85 my $checks = $validator->checks;
86 $validator = $validator->checks({size => sub ($v, $name, $value, @args) {...}});
87
88 Registered validation checks, by default only "equal_to", "in", "like",
89 "num", "size" and "upload" are already defined.
90
91 filters
92 my $filters = $validator->filters;
93 $validator = $validator->filters({trim => sub {...}});
94
95 Registered filters, by default only "comma_separated", "not_empty" and
96 "trim" are already defined.
97
99 Mojolicious::Validator inherits all methods from Mojo::Base and
100 implements the following new ones.
101
102 add_check
103 $validator = $validator->add_check(size => sub ($v, $name, $value, @args) {...});
104
105 Register a validation check.
106
107 $validator->add_check(foo => sub ($v, $name, $value, @args) {
108 ...
109 return undef;
110 });
111
112 add_filter
113 $validator = $validator->add_filter(trim => sub ($v, $name, $value) {...});
114
115 Register a new filter.
116
117 $validator->add_filter(foo => sub ($v, $name, $value) {
118 ...
119 return $value;
120 });
121
122 new
123 my $validator = Mojolicious::Validator->new;
124
125 Construct a new Mojolicious::Validator object.
126
127 validation
128 my $v = $validator->validation;
129
130 Build Mojolicious::Validator::Validation object to perform validations.
131
132 my $v = $validator->validation;
133 $v->input({foo => 'bar'});
134 $v->required('foo')->size(1, 5);
135 say $v->param('foo');
136
138 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
139
140
141
142perl v5.36.0 2023-01-20 Mojolicious::Validator(3)