1Mojolicious::Validator(U3s)er Contributed Perl DocumentatMioojnolicious::Validator(3)
2
3
4

NAME

6       Mojolicious::Validator - Validate values
7

SYNOPSIS

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

DESCRIPTION

18       Mojolicious::Validator validates values for Mojolicious.
19

CHECKS

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
51       String value length or size of Mojo::Upload object in bytes needs to be
52       between these two values.
53
54   upload
55         $v = $v->upload;
56
57       Value needs to be a Mojo::Upload object, representing a file upload.
58

FILTERS

60       These filters are available by default.
61
62   trim
63         $v = $v->optional('foo', 'trim');
64
65       Trim whitespace characters from both ends of string value with "trim"
66       in Mojo::Util.
67

ATTRIBUTES

69       Mojolicious::Validator implements the following attributes.
70
71   checks
72         my $checks = $validator->checks;
73         $validator = $validator->checks({size => sub {...}});
74
75       Registered validation checks, by default only "equal_to", "in", "like",
76       "num", "size" and "upload" are already defined.
77

METHODS

79       Mojolicious::Validator inherits all methods from Mojo::Base and
80       implements the following new ones.
81
82   add_check
83         $validator = $validator->add_check(size => sub {...});
84
85       Register a validation check.
86
87         $validator->add_check(foo => sub {
88           my ($v, $name, $value, @args) = @_;
89           ...
90           return undef;
91         });
92
93   add_filter
94         $validator = $validator->add_filter(trim => sub {...});
95
96       Register a new filter.
97
98         $validator->add_filter(foo => sub {
99           my ($v, $name, $value) = @_;
100           ...
101           return $value;
102         });
103
104   new
105         my $validator = Mojolicious::Validator->new;
106
107       Construct a new Mojolicious::Validator object.
108
109   validation
110         my $v = $validator->validation;
111
112       Build Mojolicious::Validator::Validation object to perform validations.
113
114         my $v = $validator->validation;
115         $v->input({foo => 'bar'});
116         $v->required('foo')->size(1, 5);
117         say $v->param('foo');
118

SEE ALSO

120       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
121
122
123
124perl v5.30.1                      2020-01-30         Mojolicious::Validator(3)
Impressum