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 {...}});
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 {...});
99
100 Register a validation check.
101
102 $validator->add_check(foo => sub {
103 my ($v, $name, $value, @args) = @_;
104 ...
105 return undef;
106 });
107
108 add_filter
109 $validator = $validator->add_filter(trim => sub {...});
110
111 Register a new filter.
112
113 $validator->add_filter(foo => sub {
114 my ($v, $name, $value) = @_;
115 ...
116 return $value;
117 });
118
119 new
120 my $validator = Mojolicious::Validator->new;
121
122 Construct a new Mojolicious::Validator object.
123
124 validation
125 my $v = $validator->validation;
126
127 Build Mojolicious::Validator::Validation object to perform validations.
128
129 my $v = $validator->validation;
130 $v->input({foo => 'bar'});
131 $v->required('foo')->size(1, 5);
132 say $v->param('foo');
133
135 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
136
137
138
139perl v5.32.0 2020-07-28 Mojolicious::Validator(3)