| 1 | package HTML::FormHandlerX::JQueryRemoteValidator; | ||||||||||||||||||||||
| 2 | 2 | $HTML::FormHandlerX::JQueryRemoteValidator::VERSION = '0.24'; | |||||||||||||||||||||
| 3 | 2 | ▶ | use HTML::FormHandler::Moose::Role; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 2 | ▶ | use Method::Signatures::Simple; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 2 | ▶ | use JSON (); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | |||||||||||||||||||||||
| 8 | 2 | has_field _validation_scripts => (type => 'JavaScript', set_js_code => '_js_code_for_validation_scripts'); | |||||||||||||||||||||
| 9 | |||||||||||||||||||||||
| 10 | 2 | has validation_endpoint => (is => 'rw', isa => 'Str', default => '/ajax/formvalidator'); | |||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | 2 | has jquery_validator_link => (is => 'rw', isa => 'Str', default => 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js'); | |||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 1 | ▶ | has skip_remote_validation_types => (is => 'rw', isa => 'ArrayRef', default => sub { [ qw(Submit Hidden noCAPTCHA Display JSON JavaScript) ] }); | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | 2 | has skip_all_remote_validation => (is => 'rw', isa => 'Bool', default => 0); | |||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | 0 | ▶ | method _js_code_for_validation_scripts () { | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
Subroutine
__ANON__: not called CC=3 SCAR=15.1
Subroutine
_js_code_for_validation_scripts: called CC=3 SCAR=15.1
| |||||||||||||||||||||||
| 19 | 0 | ▶ | return '' if $self->skip_all_remote_validation; | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 20 | 0 | my $spec_data = $self->_data_for_validation_spec; | |||||||||||||||||||||
| 21 | 0 | ▶ | my $spec = JSON->new->utf8 | ||||||||||||||||||||
|
Condition25%1/4
'JSON'->new->utf8->allow_nonref->pretty(1)->encode($spec_data) || ''
'JSON'->new->utf8->allow_nonref->pretty(1)->encode($spec_data) || ''
MC/DC0%0/1
'JSON'->new->utf8->allow_nonref->pretty(1)->encode($spec_data) || ''
'JSON'->new->utf8->allow_nonref->pretty(1)->encode($spec_data)
MC/DC0%0/1
'JSON'->new->utf8->allow_nonref->pretty(1)->encode($spec_data) || ''
'JSON'->new->utf8->allow_nonref->pretty(1)->encode($spec_data)
Truth table0%0/2
A
A= 'JSON'->new->utf8->allow_nonref->pretty(1)->encode($spec_data)
Truth table50%1/2
A
A= 'JSON'->new->utf8->allow_nonref->pretty(1)->encode($spec_data)
| |||||||||||||||||||||||
| 22 | ->allow_nonref | ||||||||||||||||||||||
| 23 | ->pretty(1) | ||||||||||||||||||||||
| 24 | # ->relaxed(undef) | ||||||||||||||||||||||
| 25 | # ->canonical(undef) | ||||||||||||||||||||||
| 26 | ->encode($spec_data) | ||||||||||||||||||||||
| 27 | || ''; | ||||||||||||||||||||||
| 28 | |||||||||||||||||||||||
| 29 | 0 | my $form_name = $self->name; | |||||||||||||||||||||
| 30 | 0 | $spec =~ s/"${form_name}_data_collector"/${form_name}_data_collector/g; | |||||||||||||||||||||
| 31 | 0 | $spec =~ s/\n$//; | |||||||||||||||||||||
| 32 | 0 | $spec = "\n var ${form_name}_validation_spec = $spec;\n"; | |||||||||||||||||||||
| 33 | 0 | return $self->_data_collector_script . $spec . $self->_run_validator_script; | |||||||||||||||||||||
| 34 | 2 | } | |||||||||||||||||||||
| 35 | |||||||||||||||||||||||
| 36 | 2 | ▶ | method _data_for_validation_spec () { | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=5 SCAR=17.1
| |||||||||||||||||||||||
| 37 | 3 | my $js_profile = { rules => {}, messages => {} }; | |||||||||||||||||||||
| 38 | |||||||||||||||||||||||
| 39 | 3 | foreach my $f (@{$self->fields}) { | |||||||||||||||||||||
| 40 | 24 | ▶ | next if $self->_skip_remote_validation($f); # don't build rules for these fields | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 41 | 15 | ▶ | if ($f->isa('HTML::FormHandler::Field::Repeatable')) { # also 'Compound' as well but I don't have one of these lying around just yet | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 42 | 0 | foreach my $subf (sort {$a->name cmp $b->name} map {$_->fields} $f->fields) { | |||||||||||||||||||||
| 43 | 0 | $js_profile->{rules}->{$subf->id}->{remote} = $self->_build_remote_rule($subf); | |||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||
| 46 | else { | ||||||||||||||||||||||
| 47 | 15 | $js_profile->{rules}->{$f->id}->{remote} = $self->_build_remote_rule($f); | |||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||
| 50 | |||||||||||||||||||||||
| 51 | 3 | return $js_profile; | |||||||||||||||||||||
| 52 | 2 | } | |||||||||||||||||||||
| 53 | |||||||||||||||||||||||
| 54 | 2 | ▶ | method _build_remote_rule ($field) { | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 55 | 15 | my $remote_rule = { | |||||||||||||||||||||
| 56 | url => sprintf("%s/%s/%s", $self->validation_endpoint, $self->name, $field->id), | ||||||||||||||||||||||
| 57 | type => 'POST', | ||||||||||||||||||||||
| 58 | data => $self->name . "_data_collector", | ||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||
| 60 | |||||||||||||||||||||||
| 61 | 15 | return $remote_rule; | |||||||||||||||||||||
| 62 | 2 | } | |||||||||||||||||||||
| 63 | |||||||||||||||||||||||
| 64 | 2 | ▶ | method _data_collector_script () { | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
_data_collector_script: called CC=5 SCAR=17.0
| |||||||||||||||||||||||
| 65 | 2 | my @func; | |||||||||||||||||||||
| 66 | 2 | foreach my $f (sort {$a->name cmp $b->name} $self->fields) { # the sort is for consistent output in tests | |||||||||||||||||||||
| 67 | 16 | ▶ | next if $self->_skip_data_collection($f); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 68 | 12 | ▶ | if ($f->isa('HTML::FormHandler::Field::Repeatable')) { # also 'Compound' as well but I don't have one of these lying around just yet | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 69 | 0 | foreach my $subf (sort {$a->name cmp $b->name} map {$_->fields} $f->fields) { | |||||||||||||||||||||
| 70 | 0 | push @func, sprintf " \"%s\": function () { return \$(\"#%s\").val() }", | |||||||||||||||||||||
| 71 | $subf->id, _escape_dots($subf->id); | ||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||
| 74 | else { | ||||||||||||||||||||||
| 75 | 12 | push @func, sprintf " \"%s\": function () { return \$(\"#%s\").val() }", | |||||||||||||||||||||
| 76 | $f->id, _escape_dots($f->id); | ||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | 2 | my $form_name = $self->name; | |||||||||||||||||||||
| 81 | 2 | return " var ${form_name}_data_collector = {\n" . join(",\n", @func) . "\n };\n"; | |||||||||||||||||||||
| 82 | 2 | } | |||||||||||||||||||||
| 83 | |||||||||||||||||||||||
| 84 | 12 | ▶ | sub _escape_dots { | ||||||||||||||||||||
|
Subroutine
_escape_dots: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 85 | 12 | my $str = shift; | |||||||||||||||||||||
| 86 | 12 | $str =~ s/\./\\\\./g; | |||||||||||||||||||||
| 87 | 12 | return $str; | |||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||
| 89 | |||||||||||||||||||||||
| 90 | 2 | ▶ | method _skip_remote_validation ($field) { | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||
| 91 | 38 | ▶ | return 1 if $self->skip_all_remote_validation; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 92 | 38 | my %skip_type = map {$_=>1} @{$self->skip_remote_validation_types}; | |||||||||||||||||||||
| 93 | 38 | ▶ | return 1 if $field->get_tag('no_remote_validate'); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 94 | 38 | ▶ | return 1 if $skip_type{$field->type}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 95 | 25 | return 0; | |||||||||||||||||||||
| 96 | 2 | } | |||||||||||||||||||||
| 97 | |||||||||||||||||||||||
| 98 | 2 | ▶ | method _skip_data_collection ($field) { | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
_skip_data_collection: called CC=2 SCAR=6.9
| |||||||||||||||||||||||
| 99 | 16 | ▶ | return 0 if $field->type eq 'Hidden'; # collect, but don't validate, hidden fields | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 100 | 14 | return $self->_skip_remote_validation($field); | |||||||||||||||||||||
| 101 | 2 | } | |||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | 2 | ▶ | method _run_validator_script () { | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
_run_validator_script: called CC=2 SCAR=7.0
| |||||||||||||||||||||||
| 104 | 2 | my $form_name = $self->name; | |||||||||||||||||||||
| 105 | 2 | my $link = $self->jquery_validator_link; | |||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | my $opts = join ",\n ", | ||||||||||||||||||||||
| 108 | 0 | map { sprintf "%s: %s", $_, $self->jquery_validator_opts->{$_} } | |||||||||||||||||||||
| 109 | 2 | keys %{$self->jquery_validator_opts}; | |||||||||||||||||||||
| 110 | |||||||||||||||||||||||
| 111 | 2 | ▶ | $opts = "\n$opts," if $opts; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 112 | |||||||||||||||||||||||
| 113 | 2 | my $script = <<SCRIPT; | |||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | \$(document).ready(function() { | ||||||||||||||||||||||
| 116 | \$.getScript("$link", function () { | ||||||||||||||||||||||
| 117 | if (typeof ${form_name}_validation_spec !== 'undefined') { | ||||||||||||||||||||||
| 118 | \$('form#$form_name').validate({$opts | ||||||||||||||||||||||
| 119 | rules: ${form_name}_validation_spec.rules, | ||||||||||||||||||||||
| 120 | submitHandler: function(form) { form.submit(); } | ||||||||||||||||||||||
| 121 | }); | ||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||
| 123 | }); | ||||||||||||||||||||||
| 124 | }); | ||||||||||||||||||||||
| 125 | SCRIPT | ||||||||||||||||||||||
| 126 | |||||||||||||||||||||||
| 127 | 2 | return $script; | |||||||||||||||||||||
| 128 | 2 | } | |||||||||||||||||||||
| 129 | |||||||||||||||||||||||
| 130 | # http://jqueryvalidation.org/validate/ - start reading with the summary at the *end* of the page | ||||||||||||||||||||||
| 131 | 1 | ▶ | has 'jquery_validator_opts' => (is => 'rw', isa => 'HashRef[Str]', required => 0, default => sub {{}}); | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 132 | |||||||||||||||||||||||
| 133 | |||||||||||||||||||||||
| 134 | |||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | |||||||||||||||||||||||
| 137 | |||||||||||||||||||||||
| 138 | 2 | 1; # End of HTML::FormHandlerX::JQueryRemoteValidator | |||||||||||||||||||||
| 139 | |||||||||||||||||||||||
| 140 | __END__ | ||||||||||||||||||||||