line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Validator; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
985
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
37
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
33
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
4
|
use base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
102
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
6
|
use Mojo::ByteStream; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
41
|
|
9
|
1
|
|
|
1
|
|
3
|
use Mojo::Loader qw(load_class); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
45
|
|
10
|
1
|
|
|
1
|
|
5
|
use MojoX::Validator; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
427
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
require Carp; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub register { |
15
|
1
|
|
|
1
|
1
|
33
|
my ($self, $app, $conf) = @_; |
16
|
|
|
|
|
|
|
|
17
|
1
|
|
50
|
|
|
3
|
$conf ||= {}; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$app->helper( |
20
|
|
|
|
|
|
|
create_validator => sub { |
21
|
12
|
|
|
12
|
|
253541
|
my $self = shift; |
22
|
12
|
|
|
|
|
20
|
my $class_name = shift; |
23
|
|
|
|
|
|
|
|
24
|
12
|
|
100
|
|
|
52
|
$class_name ||= 'MojoX::Validator'; |
25
|
|
|
|
|
|
|
|
26
|
12
|
100
|
|
|
|
44
|
unless ($class_name =~ m/[A-Z]/) { |
27
|
3
|
|
|
|
|
47
|
my $namespace = ref($self->app) . '::'; |
28
|
3
|
50
|
|
|
|
23
|
$namespace = '' if $namespace =~ m/^Mojolicious::Lite/; |
29
|
|
|
|
|
|
|
|
30
|
3
|
|
|
|
|
17
|
$class_name = join '' => $namespace, |
31
|
|
|
|
|
|
|
Mojo::ByteStream->new($class_name)->camelize; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
12
|
|
|
|
|
213
|
my $e = load_class($class_name); |
35
|
|
|
|
|
|
|
|
36
|
12
|
50
|
|
|
|
259
|
Carp::croak qq/Can't load validator '$class_name': / . $e->message |
37
|
|
|
|
|
|
|
if ref $e; |
38
|
|
|
|
|
|
|
|
39
|
12
|
50
|
|
|
|
30
|
Carp::croak qq/Can't find validator '$class_name'/ if $e; |
40
|
|
|
|
|
|
|
|
41
|
12
|
100
|
|
|
|
307
|
Carp::croak qq/Wrong validator '$class_name' isa/ |
42
|
|
|
|
|
|
|
unless $class_name->isa('MojoX::Validator'); |
43
|
|
|
|
|
|
|
|
44
|
11
|
|
|
|
|
39
|
return $class_name->new(%$conf, @_); |
45
|
|
|
|
|
|
|
} |
46
|
1
|
|
|
|
|
9
|
); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
$app->helper( |
49
|
|
|
|
|
|
|
validate => sub { |
50
|
10
|
|
|
10
|
|
4419
|
my $self = shift; |
51
|
10
|
|
|
|
|
15
|
my $validator = shift; |
52
|
10
|
|
|
|
|
10
|
my $params = shift; |
53
|
|
|
|
|
|
|
|
54
|
10
|
|
33
|
|
|
41
|
$params ||= $self->req->params->to_hash; |
55
|
|
|
|
|
|
|
|
56
|
10
|
100
|
|
|
|
3851
|
return 1 if $validator->validate($params); |
57
|
|
|
|
|
|
|
|
58
|
7
|
|
|
|
|
1414
|
$self->stash(validator_errors => $validator->errors); |
59
|
7
|
|
|
|
|
148
|
$self->stash(validator_has_unknown_params => |
60
|
|
|
|
|
|
|
$validator->has_unknown_params); |
61
|
|
|
|
|
|
|
|
62
|
7
|
|
|
|
|
95
|
return; |
63
|
|
|
|
|
|
|
} |
64
|
1
|
|
|
|
|
86
|
); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
$app->helper(validator_has_unknown_params => |
67
|
1
|
|
|
1
|
|
57
|
sub { shift->stash('validator_has_unknown_params') }); |
|
1
|
|
|
|
|
2445
|
|
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
|
|
|
|
|
62
|
); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
$app->helper( |
82
|
|
|
|
|
|
|
validator_error => sub { |
83
|
6
|
|
|
6
|
|
15927
|
my $self = shift; |
84
|
6
|
|
|
|
|
10
|
my $name = shift; |
85
|
|
|
|
|
|
|
|
86
|
6
|
50
|
|
|
|
16
|
return unless my $errors = $self->stash('validator_errors'); |
87
|
|
|
|
|
|
|
|
88
|
6
|
50
|
|
|
|
74
|
return unless my $message = $errors->{$name}; |
89
|
|
|
|
|
|
|
|
90
|
6
|
|
|
|
|
58
|
return $self->tag('div' => class => 'error' => sub {$message}); |
|
6
|
|
|
|
|
556
|
|
91
|
|
|
|
|
|
|
} |
92
|
1
|
|
|
|
|
56
|
); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
__END__ |