File Coverage

blib/lib/SparkX/Form/Field/Validator/Regex.pm
Criterion Covered Total %
statement 8 8 100.0
branch 3 4 75.0
condition n/a
subroutine 2 2 100.0
pod n/a
total 13 14 92.8


line stmt bran cond sub pod time code
1             package SparkX::Form::Field::Validator::Regex;
2             our $VERSION = '0.2102';
3              
4              
5             # ABSTRACT: Validates a field matches a regular expression
6              
7 2     2   1058 use Moose::Role;
  2         2  
  2         17  
8              
9             has regex => (
10             isa => 'Maybe[RegexpRef]',
11             is => 'rw',
12             required => 0,
13             default => undef,
14             );
15              
16             has errmsg_regex => (
17             isa => 'Str',
18             is => 'rw',
19             required => 0,
20             lazy => 1,
21             default => sub {
22             my $self = shift;
23             $self->human_name . ' failed the regex.'
24             },
25             );
26              
27             sub _regex {
28 7     7   11 my ($self) = @_;
29              
30 7 50       228 return unless $self->regex;
31              
32 7 100       173 if ($self->value !~ $self->regex) {
33 5         175 $self->error($self->errmsg_regex);
34             }
35 7         19 return $self;
36             }
37              
38             after '_validate' => sub { return shift->_regex };
39              
40             1;
41              
42              
43              
44             =pod
45              
46             =head1 NAME
47              
48             SparkX::Form::Field::Validator::Regex - Validates a field matches a regular expression
49              
50             =head1 VERSION
51              
52             version 0.2102
53              
54             =head1 DESCRIPTION
55              
56             A regular expression validation mix-in. Adds two fields plus action.
57             Makes sure that C<value> matches the expression.
58              
59             =head1 ACCESSORS
60              
61             =head2 C<regex> => Str
62              
63             RegexRef to match.
64             Required, no default.
65              
66             =head2 errmsg_regex => Str
67              
68             Allows you to provide a custom error message for when the match fails.
69             Required, no default.
70              
71              
72              
73             =head1 AUTHOR
74              
75             James Laver L<http://jameslaver.com>
76              
77             =head1 COPYRIGHT AND LICENSE
78              
79             This software is copyright (c) 2009 by James Laver C<< <sprintf qw(%s@%s.%s cpan jameslaver com)> >>.
80              
81             This is free software; you can redistribute it and/or modify it under
82             the same terms as the Perl 5 programming language system itself.
83              
84             =cut
85              
86              
87              
88             __END__
89