| 1 | package HTML::Widget::Constraint::Range; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use base 'HTML::Widget::Constraint'; | ||
| 6 | |||
| 7 | use Scalar::Util qw(looks_like_number); | ||
| 8 | |||
| 9 | __PACKAGE__->mk_accessors(qw/minimum maximum/); | ||
| 10 | |||
| 11 | *min = \&minimum; | ||
| 12 | *max = \&maximum; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | HTML::Widget::Constraint::Range - Range Constraint | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | my $c = $widget->constraint( 'Range', 'foo' ); | ||
| 21 | $c->min(23); | ||
| 22 | $c->max(30); | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | Range Constraint. | ||
| 27 | |||
| 28 | =head1 METHODS | ||
| 29 | |||
| 30 | =head2 maximum | ||
| 31 | |||
| 32 | Arguments: $max_value | ||
| 33 | |||
| 34 | =head2 minimum | ||
| 35 | |||
| 36 | Arguments: $min_value | ||
| 37 | |||
| 38 | =head2 validate | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | sub validate { | ||
| 43 | my ( $self, $value ) = @_; | ||
| 44 | |||
| 45 | return 1 if !defined $value || $value eq ''; | ||
| 46 | |||
| 47 | my $minimum = $self->minimum; | ||
| 48 | my $maximum = $self->maximum; | ||
| 49 | my $failed = 0; | ||
| 50 | |||
| 51 | $failed++ if !looks_like_number($value); | ||
| 52 | |||
| 53 | if ( !$failed && defined $minimum ) { | ||
| 54 | $failed++ unless ( $value >= $minimum ); | ||
| 55 | } | ||
| 56 | if ( !$failed && defined $maximum ) { | ||
| 57 | $failed++ unless ( $value <= $maximum ); | ||
| 58 | } | ||
| 59 | return !$failed; | ||
| 60 | } | ||
| 61 | |||
| 62 | =head1 AUTHOR | ||
| 63 | |||
| 64 | Sebastian Riedel, C<sri@oook.de> | ||
| 65 | |||
| 66 | =head1 LICENSE | ||
| 67 | |||
| 68 | This library is free software, you can redistribute it and/or modify it under | ||
| 69 | the same terms as Perl itself. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | 1; |