line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::FormHandlerX::JQueryRemoteValidator; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
3843448
|
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.03 |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=cut |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
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
|
|
|
|
|
|
|
has element_target_class => (is => 'ro', isa => 'Str', default => 'form-group'); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
method _js_code_for_validation_scripts () { |
63
|
|
|
|
|
|
|
my $spec_data = $self->_data_for_validation_spec; |
64
|
|
|
|
|
|
|
my $spec = JSON->new->utf8 |
65
|
|
|
|
|
|
|
->allow_nonref |
66
|
|
|
|
|
|
|
->pretty(1) |
67
|
|
|
|
|
|
|
# ->relaxed(undef) |
68
|
|
|
|
|
|
|
# ->canonical(undef) |
69
|
|
|
|
|
|
|
->encode($spec_data) |
70
|
|
|
|
|
|
|
|| ''; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my $form_name = $self->name; |
73
|
|
|
|
|
|
|
$spec =~ s/"${form_name}_data_collector"/${form_name}_data_collector/g; |
74
|
|
|
|
|
|
|
$spec =~ s/\n$//; |
75
|
|
|
|
|
|
|
$spec = "\n var ${form_name}_validation_spec = $spec;\n"; |
76
|
|
|
|
|
|
|
return $self->_data_collector_script . $spec . $self->_run_validator_script; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
method _data_for_validation_spec () { |
80
|
|
|
|
|
|
|
my $js_profile = { rules => {}, messages => {} }; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
foreach my $field (@{$self->fields}) { |
83
|
|
|
|
|
|
|
next if $self->_skip_remote_validation($field); # don't build rules for these fields |
84
|
|
|
|
|
|
|
$js_profile->{rules}->{$field->id}->{remote} = $self->_build_remote_rule($field); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
return $js_profile; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
method _build_remote_rule ($field) { |
91
|
|
|
|
|
|
|
my $remote_rule = { |
92
|
|
|
|
|
|
|
url => sprintf("%s/%s/%s", $self->validation_endpoint, $self->name, $field->name), |
93
|
|
|
|
|
|
|
type => 'POST', |
94
|
|
|
|
|
|
|
data => $self->name . "_data_collector", |
95
|
|
|
|
|
|
|
}; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
return $remote_rule; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
method _data_collector_script () { |
101
|
|
|
|
|
|
|
my $script = join(",\n", |
102
|
|
|
|
|
|
|
map { sprintf " \"%s.%s\": function () { return \$(\"#%s\\\\.%s\").val() }", $self->name, $_->name, $self->name, $_->name } |
103
|
|
|
|
|
|
|
grep { ! $self->_skip_remote_validation($_) } |
104
|
|
|
|
|
|
|
sort {$a->name cmp $b->name} # the sort is there to keep output consistent for test scripts |
105
|
|
|
|
|
|
|
$self->fields |
106
|
|
|
|
|
|
|
); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
my $form_name = $self->name; |
109
|
|
|
|
|
|
|
return " var ${form_name}_data_collector = {\n" . $script . "\n };\n"; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
method _skip_remote_validation ($field) { |
113
|
|
|
|
|
|
|
return 1 if $self->skip_all_remote_validation; |
114
|
|
|
|
|
|
|
my %skip_field = map {$_=>1} @{$self->skip_remote_validation_fields}; |
115
|
|
|
|
|
|
|
my %skip_type = map {$_=>1} @{$self->skip_remote_validation_types}; |
116
|
|
|
|
|
|
|
return 1 if $skip_field{$field->name}; |
117
|
|
|
|
|
|
|
return 1 if $skip_type{$field->type}; |
118
|
|
|
|
|
|
|
return 0; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
method _run_validator_script () { |
122
|
|
|
|
|
|
|
my $form_name = $self->name; |
123
|
|
|
|
|
|
|
my $link = $self->jquery_validator_link; |
124
|
|
|
|
|
|
|
my $css_target = $self->element_target_class; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my $script = < |