line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::FormHandlerX::Form::JQueryValidator; |
2
|
1
|
|
|
1
|
|
48353
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
37
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
46
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
HTML::FormHandlerX::Form::JQueryValidator - Perl trait for HTML::FormHandler and JQuery Validator |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 VERSION |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
0.05 |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=cut |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
1
|
|
4642
|
use JSON; |
|
1
|
|
|
|
|
50392
|
|
|
1
|
|
|
|
|
6
|
|
19
|
1
|
|
|
1
|
|
1109
|
use URI::Escape; |
|
1
|
|
|
|
|
1581
|
|
|
1
|
|
|
|
|
93
|
|
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
|
3530
|
use Moose::Role; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 SYNOPSIS |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
use HTML::FormHandler::Moose; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
with HTML::FormHandlerX::Form::JQueryValidator; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
... |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
$form->to_jquery_validation_profile(); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
.... |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
<input type="hidden" id="validation_json" value="[% form.as_escaped_json %]"> |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
<script> |
38
|
|
|
|
|
|
|
var validationJSON = JSON.parse(decodeURIComponent($("#validation_json").val() ) ); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
$("#story_form").validate({ |
41
|
|
|
|
|
|
|
rules: validationJSON.rules, |
42
|
|
|
|
|
|
|
highlight: function(label) { |
43
|
|
|
|
|
|
|
$(label).closest('.control-group').addClass('error'); |
44
|
|
|
|
|
|
|
}, |
45
|
|
|
|
|
|
|
messages: validationJSON.messages, |
46
|
|
|
|
|
|
|
success: function(label) { |
47
|
|
|
|
|
|
|
label |
48
|
|
|
|
|
|
|
.text('OK!').addClass('valid') |
49
|
|
|
|
|
|
|
.closest('.control-group').addClass('success'); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
}); |
52
|
|
|
|
|
|
|
}); |
53
|
|
|
|
|
|
|
</script> |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 DESCRIPTION |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
This perl role allows you to re-use some form validation rules with the |
58
|
|
|
|
|
|
|
JQuery Validation plugin (http://docs.jquery.com/Plugins/Validation) |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 METHODS |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 to_jquery_validation_profile |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Object method, takes no arguments. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Returns as hashref holding a hash of rules and another of messages for the JQuery Validation plugin, based on the form fields of the object. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=cut |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub to_jquery_validation_profile { |
73
|
|
|
|
|
|
|
my $self = shift; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
my $js_profile = { rules => {}, messages => {} }; |
76
|
|
|
|
|
|
|
foreach my $field ( @{$self->fields}) { |
77
|
|
|
|
|
|
|
my $field_rule = { }; |
78
|
|
|
|
|
|
|
if ($field->required) { |
79
|
|
|
|
|
|
|
$field_rule->{required} = 1; |
80
|
|
|
|
|
|
|
$js_profile->{messages}{$field->id} = $self->_localize($field->get_message('required'), $field->loc_label); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
if (lc($field->type) eq 'email') { |
83
|
|
|
|
|
|
|
$field_rule->{email} = 1; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
if (lc($field->type) eq 'hour') { |
86
|
|
|
|
|
|
|
$field_rule->{range} = [0,23]; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
if (lc($field->type) eq 'minute' or lc($field->type) eq 'second' ) { |
89
|
|
|
|
|
|
|
$field_rule->{range} = [0,59]; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
if (lc($field->type) eq 'hour') { |
92
|
|
|
|
|
|
|
$field_rule->{range} = [0,23]; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
if (lc($field->type) eq 'month') { |
95
|
|
|
|
|
|
|
$field_rule->{range} = [1,12]; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
if (lc($field->type) eq 'monthday') { |
98
|
|
|
|
|
|
|
$field_rule->{range} = [1,31]; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
if (lc($field->type) =~ 'url') { |
101
|
|
|
|
|
|
|
$field_rule->{url} = 1; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
$js_profile->{rules}{$field->id} = $field_rule; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
return $js_profile; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head2 as_escaped_json |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Object method, takes no arguments. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Returns the jquery validation profile as a URI escaped json string, allowing it to be stashed |
113
|
|
|
|
|
|
|
in a hidden form field and extracted by javascript for use with JQuery Validation plugin |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=cut |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub as_escaped_json { |
118
|
|
|
|
|
|
|
my $self = shift; |
119
|
|
|
|
|
|
|
my $js_profile = $self->to_jquery_validation_profile; |
120
|
|
|
|
|
|
|
return uri_escape_utf8(JSON->new->encode($js_profile)), |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 SEE ALSO |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=over 4 |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item http://alittlecode.com/files/jQuery-Validate-Demo/ |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item http://docs.jquery.com/Plugins/Validation |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item HTML::FormHandler |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item Twitter Bootstrap |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item examples/ dir in source code |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=back |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 AUTHOR |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Aaron Trevena, E<lt>teejay@cpan.orgE<gt> |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Copyright (C) 2012 by Aaron Trevena |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
149
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.10.0 or, |
150
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=cut |