File Coverage

blib/lib/SparkX/Form/Field/Validator/MaxLength.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::MaxLength;
2             our $VERSION = '0.2102';
3              
4              
5             # ABSTRACT: Validates a variable does not exceed a given size
6              
7 2     2   1016 use Moose::Role;
  2         3  
  2         31  
8              
9             has max_length => (
10             isa => 'Maybe[Int]',
11             is => 'rw',
12             required => 0,
13             default => 0,
14             );
15              
16             has errmsg_too_long => (
17             isa => 'Str',
18             is => 'rw',
19             required => 0,
20             lazy => 1,
21             default => sub {
22             my $self = shift;
23             return $self->human_name .
24             ' must be no more than ' .
25             $self->max_length .
26             ' characters long';
27             },
28             );
29              
30             sub _max_length {
31 7     7   12 my ($self) = @_;
32              
33 7 50       242 return unless $self->max_length;
34              
35 7 100       195 if (length $self->value > $self->max_length) {
36 2         80 $self->error($self->errmsg_too_long);
37             }
38 7         15 return $self;
39             }
40              
41             after '_validate' => sub { return shift->_max_length };
42              
43             1;
44              
45              
46              
47             =pod
48              
49             =head1 NAME
50              
51             SparkX::Form::Field::Validator::MaxLength - Validates a variable does not exceed a given size
52              
53             =head1 VERSION
54              
55             version 0.2102
56              
57             =head1 DESCRIPTION
58              
59             A maximum length enforcement mix-in. Adds two fields plus action.
60             Makes sure that C<value> is at most C<max_length> characters long.
61              
62             =head1 ACCESSORS
63              
64             =head2 max_length => Int
65              
66             The maximum length you desire. Required. In a subclass, you can:
67              
68             has '+max_length' => (required => 0,default => sub { $HOW_LONG });
69              
70             if you want to have it optional with a default
71              
72             =head2 errmsg_too_long => Str
73              
74             Error message to be shown if C<value> is too long.
75              
76              
77              
78             =head1 AUTHOR
79              
80             James Laver L<http://jameslaver.com>
81              
82             =head1 COPYRIGHT AND LICENSE
83              
84             This software is copyright (c) 2009 by James Laver C<< <sprintf qw(%s@%s.%s cpan jameslaver com)> >>.
85              
86             This is free software; you can redistribute it and/or modify it under
87             the same terms as the Perl 5 programming language system itself.
88              
89             =cut
90              
91              
92              
93             __END__
94