blib/lib/Data/FormValidator/ErrMsgs/JavaScript.pm

filter: Statementstmt 48.8% 21 / 43 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 38.8% 28 / 72 CC 11 SCAR 44.5
CC
11
Cov
35%
dfv_js_error_msgs
132.0
BEGIN
1.0
BEGIN
1.0
CRAP 85.8
↑ Summary
1 package Data::FormValidator::ErrMsgs::JavaScript;
2 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use vars (qw/@EXPORT_OK/);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 @EXPORT_OK = (qw/&dfv_js_error_msgs/);
8  
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Data::FormValidator::ErrMsgs::JavaScript - Let JavaScript handle DFV error presentation.
15  
16 =cut
17  
18 2 our $VERSION = '1.00';
19  
20 =head1 SYNOPSIS
21  
22 use Data::FormValidator::ErrMsgs::JavaScript (qw/&dfv_js_error_msgs/);
23  
24 # In a DFV profile:
25 msgs => \&dfv_js_error_msgs,
26  
27 Now your error messages will come out like this:
28  
29 <script type="text/javascript">dfv_error_msg('field_name');</script>
30  
31 =head1 FUNCTIONS
32  
33 =head2 dfv_js_error_msgs
34  
35 See L<SYNOPSIS> above for syntax.
36  
37 It's up to you define the C<dfv_error_msg> function in JavaScript to do
38 something interesting.
39  
40 This function respects the following C<msgs> configuration directives,
41 which are documented more in L<Data::FormValidator>:
42  
43 prefix
44 any_errors
45  
46 Of course, these can't be set directly in the profile anymore because
47 the callback is there. They can still be set through the defaults system
48 documented in L<Data::FormValidator>.
49  
50 =head1 LIMITATIONS
51  
52 More detail could be passed through to the JavaScript function about the failure.
53 This may change in the future, perhaps in an incompatible way.
54  
55 It is not currently possible to use a format string other than the one
56 provided.
57  
58 Workarounds included sending a patch or using this routine as the basis for your own.
59  
60 That workaround might look like this:
61  
62 msgs => dfv_js_error_msgs({
63 format => 'foo: %';
64 });
65  
66 A closure could then be created which passing in the arguments
67 you want.
68  
69 =cut
70  
71 0 ▶sub dfv_js_error_msgs {
Subroutine dfv_js_error_msgs: not called CC=11 SCAR=48.8
Pod documented
72 0 my $self = shift;
73 0 ▶ my $controls = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
74 0 ▶ if (defined $controls and ref $controls ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $controls and ref $controls ne "HASH") 0 0 0
Condition0%0/3
defined $controls and ref $controls ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $controls and ref $controls ne "HASH"
defined $controls ref $controls ne "HASH"
Truth table0%0/3
A and B
A= defined $controlsB= ref $controls ne "HASH"
ABresult
0X0
100
111
75 0 die "$0: parameter passed to msgs must be a hash ref";
76 }
77  
78  
79 # Allow msgs to be called more than one to accumulate error messages
80 0 ▶ $self->{msgs} ||= {};
Condition0%0/2
$self->{"msgs"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"msgs"} ||= {}
$self->{"msgs"}
Truth table0%0/2
A
A= $self->{"msgs"}
Aresult
11
00
81 0 $self->{msgs} = { %{ $self->{msgs} }, %$controls };
82  
83 my %profile = (
84 prefix => '',
85 0 %{ $self->{msgs} },
86 # XXX Should maybe have some javascript quoting on the field name
87 format => qq{<script type="text/javascript">dfv_error_msg('%s');</script>},
88 );
89  
90  
91 0 my %msgs = ();
92  
93 # Add invalid messages to hash
94 # look at all the constraints, look up their messages (or provide a default)
95 # add field + formatted constraint message to hash
96 0 ▶ if ($self->has_invalid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_invalid) 0 0 0
97 0 my $invalid = $self->invalid;
98 0 for my $i ( keys %$invalid ) {
99 0 $msgs{$i} = sprintf $profile{format},$i;
100 }
101 }
102  
103 # Add missing messages, if any
104 0 ▶ if ($self->has_missing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_missing) 0 0 0
105 0 my $missing = $self->missing;
106 0 for my $m (@$missing) {
107 0 $msgs{$m} = sprintf $profile{format},$m;
108 }
109 }
110  
111 0 my $msgs_ref = Data::FormValidator::Results::prefix_hash($profile{prefix},\%msgs);
112  
113 0 ▶ if (! $self->success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->success) 0 0 0
114 0 ▶ $msgs_ref->{ $profile{any_errors} } = 1 if defined $profile{any_errors};
Branch: 1 branch
BranchTrueFalseTotal
if defined $profile{"any_errors"} 0 0 0
115 }
116  
117 0 return $msgs_ref;
118  
119 }
120  
121 =head1 FUTURE
122  
123 If you like this extension to Data::FormValidator, give me some feedback
124 on the Data::FormValidator list and we'll work out a stable interface.
125  
126 L<http://lists.sourceforge.net/lists/listinfo/cascade-dataform>
127  
128 =head1 AUTHOR
129  
130 Mark Stosberg, C<< <mark at summersault.com> >>
131  
132 =head1 BUGS
133  
134 Please report any bugs or feature requests to
135 C<bug-data-formvalidator-errmsgs-javascript at rt.cpan.org>, or through the web interface at
136 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-FormValidator-ErrMsgs-JavaScript>.
137 I will be notified, and then you'll automatically be notified of progress on
138 your bug as I make changes.
139  
140 =head1 SEE ALSO
141  
142  
143 L<Data::FormValidator>
144 L<http://search.cpan.org/dist/Data-FormValidator-ErrMsgs-JavaScript>
145  
146 =head1 COPYRIGHT & LICENSE
147  
148 Copyright 2005 Mark Stosberg, all rights reserved.
149  
150 This program is free software; you can redistribute it and/or modify it
151 under the same terms as Perl itself.
152  
153 =cut
154  
155 2 1; # End of Data::FormValidator::ErrMsgs::JavaScript
↑ Summary