line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::FormHandlerX::JQueryRemoteValidator; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
29278
|
use HTML::FormHandler::Moose::Role; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use Method::Signatures::Simple; |
5
|
|
|
|
|
|
|
use JSON (); |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
HTML::FormHandlerX::JQueryRemoteValidator - call server-side validation code asynchronously from client-side forms. |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 VERSION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Version 0.05 |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=cut |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
package MyApp::Form::Foo; |
23
|
|
|
|
|
|
|
use HTML::FormHandler::Moose; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
with 'HTML::FormHandlerX::JQueryRemoteValidator'; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
... |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# You need to provide a form validation script at /ajax/formvalidator |
30
|
|
|
|
|
|
|
# In Poet/Mason, something like this in /ajax/formvalidator/dhandler.mp |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
method handle () { |
33
|
|
|
|
|
|
|
$m->res->content_type('application/json'); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my ($form_name, $field_name) = split '/', $m->path_info; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $form = $.form($form_name); |
38
|
|
|
|
|
|
|
$form->process(params => $.args, no_update => 1); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $err = join ' ', @{$form->field($field_name)->errors}; |
41
|
|
|
|
|
|
|
my $result = $err || 'true'; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$m->print(JSON->new->allow_nonref->encode($result)); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=cut |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
has_field _validation_scripts => (type => 'JavaScript', set_js_code => '_js_code_for_validation_scripts'); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
has validation_endpoint => (is => 'rw', isa => 'Str', default => '/ajax/formvalidator'); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
has jquery_validator_link => (is => 'rw', isa => 'Str', default => 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js'); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
has skip_remote_validation_fields => (is => 'rw', isa => 'ArrayRef', default => sub { [ qw(submit) ] }); |
56
|
|
|
|
|
|
|
has skip_remote_validation_types => (is => 'rw', isa => 'ArrayRef', default => sub { [ qw(Hidden noCAPTCHA Display JSON JavaScript) ] }); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
has skip_all_remote_validation => (is => 'rw', isa => 'Bool', default => 0); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
method _js_code_for_validation_scripts () { |
61
|
|
|
|
|
|
|
my $spec_data = $self->_data_for_validation_spec; |
62
|
|
|
|
|
|
|
my $spec = JSON->new->utf8 |
63
|
|
|
|
|
|
|
->allow_nonref |
64
|
|
|
|
|
|
|
->pretty(1) |
65
|
|
|
|
|
|
|
# ->relaxed(undef) |
66
|
|
|
|
|
|
|
# ->canonical(undef) |
67
|
|
|
|
|
|
|
->encode($spec_data) |
68
|
|
|
|
|
|
|
|| ''; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my $form_name = $self->name; |
71
|
|
|
|
|
|
|
$spec =~ s/"${form_name}_data_collector"/${form_name}_data_collector/g; |
72
|
|
|
|
|
|
|
$spec =~ s/\n$//; |
73
|
|
|
|
|
|
|
$spec = "\n var ${form_name}_validation_spec = $spec;\n"; |
74
|
|
|
|
|
|
|
return $self->_data_collector_script . $spec . $self->_run_validator_script; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
method _data_for_validation_spec () { |
78
|
|
|
|
|
|
|
my $js_profile = { rules => {}, messages => {} }; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
foreach my $field (@{$self->fields}) { |
81
|
|
|
|
|
|
|
next if $self->_skip_remote_validation($field); # don't build rules for these fields |
82
|
|
|
|
|
|
|
$js_profile->{rules}->{$field->id}->{remote} = $self->_build_remote_rule($field); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
return $js_profile; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
method _build_remote_rule ($field) { |
89
|
|
|
|
|
|
|
my $remote_rule = { |
90
|
|
|
|
|
|
|
url => sprintf("%s/%s/%s", $self->validation_endpoint, $self->name, $field->name), |
91
|
|
|
|
|
|
|
type => 'POST', |
92
|
|
|
|
|
|
|
data => $self->name . "_data_collector", |
93
|
|
|
|
|
|
|
}; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
return $remote_rule; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
method _data_collector_script () { |
99
|
|
|
|
|
|
|
my $script = join(",\n", |
100
|
|
|
|
|
|
|
map { sprintf " \"%s.%s\": function () { return \$(\"#%s\\\\.%s\").val() }", $self->name, $_->name, $self->name, $_->name } |
101
|
|
|
|
|
|
|
grep { ! $self->_skip_remote_validation($_) } |
102
|
|
|
|
|
|
|
sort {$a->name cmp $b->name} # the sort is there to keep output consistent for test scripts |
103
|
|
|
|
|
|
|
$self->fields |
104
|
|
|
|
|
|
|
); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $form_name = $self->name; |
107
|
|
|
|
|
|
|
return " var ${form_name}_data_collector = {\n" . $script . "\n };\n"; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
method _skip_remote_validation ($field) { |
111
|
|
|
|
|
|
|
return 1 if $self->skip_all_remote_validation; |
112
|
|
|
|
|
|
|
my %skip_field = map {$_=>1} @{$self->skip_remote_validation_fields}; |
113
|
|
|
|
|
|
|
my %skip_type = map {$_=>1} @{$self->skip_remote_validation_types}; |
114
|
|
|
|
|
|
|
return 1 if $skip_field{$field->name}; |
115
|
|
|
|
|
|
|
return 1 if $skip_type{$field->type}; |
116
|
|
|
|
|
|
|
return 0; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
method _run_validator_script () { |
120
|
|
|
|
|
|
|
my $form_name = $self->name; |
121
|
|
|
|
|
|
|
my $link = $self->jquery_validator_link; |
122
|
|
|
|
|
|
|
my $js_apply_error_classes = $self->js_apply_error_classes; |
123
|
|
|
|
|
|
|
my $js_apply_success_classes = $self->js_apply_success_classes; |
124
|
|
|
|
|
|
|
chomp($js_apply_error_classes); |
125
|
|
|
|
|
|
|
chomp($js_apply_success_classes); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
my $script = < |