File Coverage

blib/lib/Data/FormValidator/ErrMsgs/JavaScript.pm
Criterion Covered Total %
statement 18 40 45.0
branch 0 10 0.0
condition 0 7 0.0
subroutine 6 7 85.7
pod 1 1 100.0
total 25 65 38.4


line stmt bran cond sub pod time code
1             package Data::FormValidator::ErrMsgs::JavaScript;
2 1     1   12952 use base 'Exporter';
  1         1  
  1         76  
3 1     1   4 use Carp;
  1         1  
  1         56  
4 1     1   4 use strict;
  1         4  
  1         23  
5 1     1   2 use vars (qw/@EXPORT_OK/);
  1         1  
  1         49  
6              
7             @EXPORT_OK = (qw/&dfv_js_error_msgs/);
8              
9 1     1   10 use warnings;
  1         1  
  1         22  
10 1     1   3 use strict;
  1         1  
  1         221  
11              
12             =head1 NAME
13              
14             Data::FormValidator::ErrMsgs::JavaScript - Let JavaScript handle DFV error presentation.
15              
16             =cut
17              
18             our $VERSION = '0.50_01';
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            
30              
31             =head1 FUNCTIONS
32              
33             =head2 dfv_js_error_msgs
34              
35             See L above for syntax.
36              
37             It's up to you define the C function in JavaScript to do
38             something interesting.
39              
40             This function respects the following C configuration directives,
41             which are documented more in L:
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.
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             sub dfv_js_error_msgs {
72 0     0 1   my $self = shift;
73 0   0       my $controls = shift || {};
74 0 0 0       if (defined $controls and ref $controls ne 'HASH') {
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   0       $self->{msgs} ||= {};
81 0           $self->{msgs} = { %{ $self->{msgs} }, %$controls };
  0            
82              
83             my %profile = (
84             prefix => '',
85 0           %{ $self->{msgs} },
  0            
86             # XXX Should maybe have some javascript quoting on the field name
87             format => qq{},
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 0         if ($self->has_invalid) {
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 0         if ($self->has_missing) {
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 0         if (! $self->success) {
114 0 0         $msgs_ref->{ $profile{any_errors} } = 1 if defined $profile{any_errors};
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
127              
128             =head1 AUTHOR
129              
130             Mark Stosberg, C<< >>
131              
132             =head1 BUGS
133              
134             Please report any bugs or feature requests to
135             C, or through the web interface at
136             L.
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
144             L
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             1; # End of Data::FormValidator::ErrMsgs::JavaScript