add files
|
1 |
package Mojolicious::Validator; |
2 |
use Mojo::Base -base; |
|
3 | ||
4 |
use Mojolicious::Validator::Validation; |
|
5 | ||
6 |
has checks => sub { |
|
7 |
{equal_to => \&_equal_to, in => \&_in, like => \&_like, size => \&_size}; |
|
8 |
}; |
|
9 | ||
10 |
sub add_check { |
|
11 |
my ($self, $name, $cb) = @_; |
|
12 |
$self->checks->{$name} = $cb; |
|
13 |
return $self; |
|
14 |
} |
|
15 | ||
16 |
sub validation { |
|
17 |
Mojolicious::Validator::Validation->new(validator => shift); |
|
18 |
} |
|
19 | ||
20 |
sub _equal_to { |
|
21 |
my ($validation, $name, $value, $to) = @_; |
|
22 |
return 1 unless defined(my $other = $validation->input->{$to}); |
|
23 |
return $value ne $other; |
|
24 |
} |
|
25 | ||
26 |
sub _in { |
|
27 |
my ($validation, $name, $value) = (shift, shift, shift); |
|
28 |
$value eq $_ && return undef for @_; |
|
29 |
return 1; |
|
30 |
} |
|
31 | ||
32 |
sub _like { $_[2] !~ $_[3] } |
|
33 | ||
34 |
sub _size { |
|
35 |
my ($validation, $name, $value, $min, $max) = @_; |
|
36 |
my $len = length $value; |
|
37 |
return $len < $min || $len > $max; |
|
38 |
} |
|
39 | ||
40 |
1; |
|
41 | ||
42 |
=encoding utf8 |
|
43 | ||
44 |
=head1 NAME |
|
45 | ||
46 |
Mojolicious::Validator - Validate form data |
|
47 | ||
48 |
=head1 SYNOPSIS |
|
49 | ||
50 |
use Mojolicious::Validator; |
|
51 | ||
52 |
my $validator = Mojolicious::Validator->new; |
|
53 |
my $validation = $validator->validation; |
|
54 |
$validation->input({foo => 'bar'}); |
|
55 |
$validation->required('foo')->like(qr/ar$/); |
|
56 |
say $validation->param('foo'); |
|
57 | ||
58 |
=head1 DESCRIPTION |
|
59 | ||
60 |
L<Mojolicious::Validator> validates form data for L<Mojolicious>. |
|
61 | ||
62 |
=head1 CHECKS |
|
63 | ||
64 |
These validation checks are available by default. |
|
65 | ||
66 |
=head2 equal_to |
|
67 | ||
68 |
$validation->equal_to('foo'); |
|
69 | ||
70 |
Value needs to be equal to the value of another field. |
|
71 | ||
72 |
=head2 in |
|
73 | ||
74 |
$validation->in(qw(foo bar baz)); |
|
75 | ||
76 |
Value needs to match one of the values in the list. |
|
77 | ||
78 |
=head2 like |
|
79 | ||
80 |
$validation->like(qr/^[A-Z]/); |
|
81 | ||
82 |
Value needs to match the regular expression. |
|
83 | ||
84 |
=head2 size |
|
85 | ||
86 |
$validation->size(2, 5); |
|
87 | ||
88 |
Value length in characters needs to be between these two values. |
|
89 | ||
90 |
=head1 ATTRIBUTES |
|
91 | ||
92 |
L<Mojolicious::Validator> implements the following attributes. |
|
93 | ||
94 |
=head2 checks |
|
95 | ||
96 |
my $checks = $validator->checks; |
|
97 |
$validator = $validator->checks({size => sub {...}}); |
|
98 | ||
99 |
Registered validation checks, by default only L</"equal_to">, L</"in">, |
|
100 |
L</"like"> and L</"size"> are already defined. |
|
101 | ||
102 |
=head1 METHODS |
|
103 | ||
104 |
L<Mojolicious::Validator> inherits all methods from L<Mojo::Base> and |
|
105 |
implements the following new ones. |
|
106 | ||
107 |
=head2 add_check |
|
108 | ||
109 |
$validator = $validator->add_check(size => sub {...}); |
|
110 | ||
111 |
Register a new validation check. |
|
112 | ||
113 |
=head2 validation |
|
114 | ||
115 |
my $validation = $validator->validation; |
|
116 | ||
117 |
Build L<Mojolicious::Validator::Validation> object to perform validations. |
|
118 | ||
119 |
my $validation = $validator->validation; |
|
120 |
$validation->input({foo => 'bar'}); |
|
121 |
$validation->required('foo')->size(1, 5); |
|
122 |
say $validation->param('foo'); |
|
123 | ||
124 |
=head1 SEE ALSO |
|
125 | ||
126 |
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>. |
|
127 | ||
128 |
=cut |