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(2, undef);
42 $v = $v->num(undef, 5);
43
44 String value needs to be a non-fractional number and if provided in the
45 given range.
46
47 size
48 $v = $v->size(2, 5);
49
50 String value length or size of Mojo::Upload object in bytes needs to be
51 between these two values.
52
53 upload
54 $v = $v->upload;
55
56 Value needs to be a Mojo::Upload object, representing a file upload.
57
59 These filters are available by default.
60
61 trim
62 $v = $v->optional('foo', 'trim');
63
64 Trim whitespace characters from both ends of string value with "trim"
65 in Mojo::Util.
66
68 Mojolicious::Validator implements the following attributes.
69
70 checks
71 my $checks = $validator->checks;
72 $validator = $validator->checks({size => sub {...}});
73
74 Registered validation checks, by default only "equal_to", "in", "like",
75 "num", "size" and "upload" are already defined.
76
78 Mojolicious::Validator inherits all methods from Mojo::Base and
79 implements the following new ones.
80
81 add_check
82 $validator = $validator->add_check(size => sub {...});
83
84 Register a validation check.
85
86 $validator->add_check(foo => sub {
87 my ($v, $name, $value, @args) = @_;
88 ...
89 return undef;
90 });
91
92 add_filter
93 $validator = $validator->add_filter(trim => sub {...});
94
95 Register a new filter.
96
97 $validator->add_filter(foo => sub {
98 my ($v, $name, $value) = @_;
99 ...
100 return $value;
101 });
102
103 new
104 my $validator = Mojolicious::Validator->new;
105
106 Construct a new Mojolicious::Validator object.
107
108 validation
109 my $v = $validator->validation;
110
111 Build Mojolicious::Validator::Validation object to perform validations.
112
113 my $v = $validator->validation;
114 $v->input({foo => 'bar'});
115 $v->required('foo')->size(1, 5);
116 say $v->param('foo');
117
119 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
120
121
122
123perl v5.28.1 2018-11-22 Mojolicious::Validator(3)