File Coverage

blib/lib/CGI/Untaint/CountyStateProvince.pm
Criterion Covered Total %
statement 17 24 70.8
branch 1 6 16.6
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 25 37 67.5


line stmt bran cond sub pod time code
1             package CGI::Untaint::CountyStateProvince;
2              
3 4     4   1248027 use warnings;
  4         26  
  4         324  
4 4     4   25 use strict;
  4         21  
  4         118  
5 4     4   30 use Carp;
  4         9  
  4         418  
6              
7 4     4   27 use base 'CGI::Untaint::object';
  4         10  
  4         2307  
8              
9             =head1 NAME
10              
11             CGI::Untaint::CountyStateProvince - Validate a state, county or province in a
12             CGI script.
13              
14             =head1 VERSION
15              
16             Version 0.07
17              
18             =cut
19              
20             our $VERSION = '0.07';
21              
22             our @countries;
23              
24             =head1 SYNOPSIS
25              
26             CGI::Untaint::CountyStateProvince is a subclass of CGI::Untaint used to
27             validate if the given user data is a valid county/state/province.
28              
29             This class is not to be instantiated, instead a subclass must be
30             instantiated. For example L would
31             validate against a British county, L
32             would validate against a US state, and so on.
33              
34             use CGI::Info;
35             use CGI::Untaint;
36             use CGI::Untaint::CountyStateProvince;
37             # ...
38             my $info = CGI::Info->new();
39             my $params = $info->params();
40             # ...
41             # Country table(s) must be loaded after CGI::Untaint::CountyStateProvince
42             if($params->{'country'} == 44) {
43             require CGI::Untaint::CountyStateProvince::GB;
44              
45             CGI::Untaint::CountyStateProvince::GB->import();
46             } elsif($params->{'country'} == 1) {
47             require CGI::Untaint::CountyStateProvince::US;
48              
49             CGI::Untaint::CountyStateProvince::US->import();
50             } else {
51             die 'Unsupported country ', $params->{'country'};
52             }
53             my $u = CGI::Untaint->new($params);
54             my $csp = $u->extract(-as_CountyStateProvince => 'state');
55             # $csp will be lower case
56              
57             =head1 SUBROUTINES/METHODS
58              
59             =head2 is_valid
60              
61             Validates the data.
62              
63             =cut
64              
65             sub _untaint_re {
66             # Only allow letters and spaces
67 8     8   201356 return qr/^([a-zA-Z\s]+)$/;
68             }
69              
70             sub is_valid {
71 3     3 1 56 my $self = shift;
72              
73 3 50       9 unless(@countries) {
74 3         34 carp 'You must specify at least one country';
75 3         569 return 0;
76             }
77              
78 0           my $value = $self->value();
79              
80 0           foreach my $country(@countries) {
81 0           $country->value($value);
82              
83 0 0         if(my $new_value = $country->is_valid()) {
84 0 0         $self->value($new_value) if $new_value ne $value;
85             } else {
86 0           return 0;
87             }
88             }
89              
90 0           return 1;
91             }
92              
93              
94             =head1 AUTHOR
95              
96             Nigel Horne, C<< >>
97              
98             =head1 BUGS
99              
100             Please report any bugs or feature requests to C, or through
101             the web interface at L. I will be notified, and then you'll
102             automatically be notified of progress on your bug as I make changes.
103              
104             =head1 SEE ALSO
105              
106             CGI::Untaint
107              
108             =head1 SUPPORT
109              
110             You can find documentation for this module with the perldoc command.
111              
112             perldoc CGI::Untaint::CountyStateProvince
113              
114             You can also look for information at:
115              
116             =over 4
117              
118             =item * RT: CPAN's request tracker
119              
120             L
121              
122             =item * Search CPAN
123              
124             L
125              
126             =back
127              
128             =head1 ACKNOWLEDGEMENTS
129              
130              
131             =head1 LICENSE AND COPYRIGHT
132              
133             Copyright 2012-2025 Nigel Horne.
134              
135             This program is released under the following licence: GPL2
136              
137             =cut
138              
139             1; # End of CGI::Untaint::CountyStateProvince