line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Validator; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1311
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
3
|
use base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
98
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
5
|
use Mojo::ByteStream; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
50
|
|
9
|
1
|
|
|
1
|
|
8
|
use Mojo::Loader; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
10
|
|
10
|
1
|
|
|
1
|
|
31
|
use MojoX::Validator; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
436
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
require Carp; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub register { |
15
|
1
|
|
|
1
|
1
|
31
|
my ($self, $app, $conf) = @_; |
16
|
|
|
|
|
|
|
|
17
|
1
|
|
50
|
|
|
3
|
$conf ||= {}; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$app->helper( |
20
|
|
|
|
|
|
|
create_validator => sub { |
21
|
12
|
|
|
12
|
|
350482
|
my $self = shift; |
22
|
12
|
|
|
|
|
27
|
my $class_name = shift; |
23
|
|
|
|
|
|
|
|
24
|
12
|
|
100
|
|
|
219
|
$class_name ||= 'MojoX::Validator'; |
25
|
|
|
|
|
|
|
|
26
|
12
|
100
|
|
|
|
55
|
unless ($class_name =~ m/[A-Z]/) { |
27
|
3
|
|
|
|
|
81
|
my $namespace = ref($self->app) . '::'; |
28
|
3
|
50
|
|
|
|
41
|
$namespace = '' if $namespace =~ m/^Mojolicious::Lite/; |
29
|
|
|
|
|
|
|
|
30
|
3
|
|
|
|
|
25
|
$class_name = join '' => $namespace, |
31
|
|
|
|
|
|
|
Mojo::ByteStream->new($class_name)->camelize; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
12
|
|
|
|
|
337
|
my $e = Mojo::Loader->new->load($class_name); |
35
|
|
|
|
|
|
|
|
36
|
12
|
50
|
|
|
|
455
|
Carp::croak qq/Can't load validator '$class_name': / . $e->message |
37
|
|
|
|
|
|
|
if ref $e; |
38
|
|
|
|
|
|
|
|
39
|
12
|
50
|
|
|
|
42
|
Carp::croak qq/Can't find validator '$class_name'/ if $e; |
40
|
|
|
|
|
|
|
|
41
|
12
|
100
|
|
|
|
298
|
Carp::croak qq/Wrong validator '$class_name' isa/ |
42
|
|
|
|
|
|
|
unless $class_name->isa('MojoX::Validator'); |
43
|
|
|
|
|
|
|
|
44
|
11
|
|
|
|
|
57
|
return $class_name->new(%$conf, @_); |
45
|
|
|
|
|
|
|
} |
46
|
1
|
|
|
|
|
10
|
); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
$app->helper( |
49
|
|
|
|
|
|
|
validate => sub { |
50
|
10
|
|
|
10
|
|
5123
|
my $self = shift; |
51
|
10
|
|
|
|
|
20
|
my $validator = shift; |
52
|
10
|
|
|
|
|
13
|
my $params = shift; |
53
|
|
|
|
|
|
|
|
54
|
10
|
|
33
|
|
|
55
|
$params ||= $self->req->params->to_hash; |
55
|
|
|
|
|
|
|
|
56
|
10
|
100
|
|
|
|
4420
|
return 1 if $validator->validate($params); |
57
|
|
|
|
|
|
|
|
58
|
7
|
|
|
|
|
1458
|
$self->stash(validator_errors => $validator->errors); |
59
|
7
|
|
|
|
|
179
|
$self->stash(validator_has_unknown_params => |
60
|
|
|
|
|
|
|
$validator->has_unknown_params); |
61
|
|
|
|
|
|
|
|
62
|
7
|
|
|
|
|
108
|
return; |
63
|
|
|
|
|
|
|
} |
64
|
1
|
|
|
|
|
91
|
); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
$app->helper(validator_has_unknown_params => |
67
|
1
|
|
|
1
|
|
56
|
sub { shift->stash('validator_has_unknown_params') }); |
|
1
|
|
|
|
|
2759
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
$app->helper( |
70
|
|
|
|
|
|
|
validator_has_errors => sub { |
71
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
0
|
my $errors = $self->stash('validator_errors'); |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
0
|
|
|
0
|
return 0 if !$errors || !keys %$errors; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
0
|
return 1; |
78
|
|
|
|
|
|
|
} |
79
|
1
|
|
|
|
|
61
|
); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
$app->helper( |
82
|
|
|
|
|
|
|
validator_error => sub { |
83
|
6
|
|
|
6
|
|
20588
|
my $self = shift; |
84
|
6
|
|
|
|
|
11
|
my $name = shift; |
85
|
|
|
|
|
|
|
|
86
|
6
|
50
|
|
|
|
74
|
return unless my $errors = $self->stash('validator_errors'); |
87
|
|
|
|
|
|
|
|
88
|
6
|
50
|
|
|
|
98
|
return unless my $message = $errors->{$name}; |
89
|
|
|
|
|
|
|
|
90
|
6
|
|
|
|
|
88
|
return $self->tag('div' => class => 'error' => sub {$message}); |
|
6
|
|
|
|
|
804
|
|
91
|
|
|
|
|
|
|
} |
92
|
1
|
|
|
|
|
57
|
); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
__END__ |