HTML::Widget::Constraint::Range - Range Constraint


HTML-Widget documentation Contained in the HTML-Widget distribution.

Index


Code Index:

NAME

Top

HTML::Widget::Constraint::Range - Range Constraint

SYNOPSIS

Top

    my $c = $widget->constraint( 'Range', 'foo' );
    $c->min(23);
    $c->max(30);

DESCRIPTION

Top

Range Constraint.

METHODS

Top

maximum

Arguments: $max_value

minimum

Arguments: $min_value

validate

AUTHOR

Top

Sebastian Riedel, sri@oook.de

LICENSE

Top

This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself.


HTML-Widget documentation Contained in the HTML-Widget distribution.
package HTML::Widget::Constraint::Range;

use warnings;
use strict;
use base 'HTML::Widget::Constraint';

use Scalar::Util qw(looks_like_number);

__PACKAGE__->mk_accessors(qw/minimum maximum/);

*min = \&minimum;
*max = \&maximum;

sub validate {
    my ( $self, $value ) = @_;

    return 1 if !defined $value || $value eq '';

    my $minimum = $self->minimum;
    my $maximum = $self->maximum;
    my $failed  = 0;

    $failed++ if !looks_like_number($value);

    if ( !$failed && defined $minimum ) {
        $failed++ unless ( $value >= $minimum );
    }
    if ( !$failed && defined $maximum ) {
        $failed++ unless ( $value <= $maximum );
    }
    return !$failed;
}

1;