Form::Factory::Feature::Control::Length - A control feature for checking length


Form-Factory documentation Contained in the Form-Factory distribution.

Index


Code Index:

NAME

Top

Form::Factory::Feature::Control::Length - A control feature for checking length

VERSION

Top

version 0.020

SYNOPSIS

Top

  has_control login_name => (
      control => 'text',
      feature => {
          length => {
              minimum => 3,
              maximum => 15,
          },
      },
  );

DESCRIPTION

Top

Linked to a control, it checks to see that the string is not too short or too long.

ATTRIBUTES

Top

minumum

The optional minimum length permitted for the string.

maximum

The optional maximum length permitted for the string.

METHODS

Top

check_control

Makes sure the value is a Form::Factory::Control::Role::ScalarValue.

check

Verifies that the value of the control is not too short or too long.

AUTHOR

Top

Andrew Sterling Hanenkamp <hanenkamp@cpan.org>

COPYRIGHT AND LICENSE

Top


Form-Factory documentation Contained in the Form-Factory distribution.
package Form::Factory::Feature::Control::Length;
BEGIN {
  $Form::Factory::Feature::Control::Length::VERSION = '0.020';
}
use Moose;

with qw( 
    Form::Factory::Feature 
    Form::Factory::Feature::Role::Check
    Form::Factory::Feature::Role::Control
    Form::Factory::Feature::Role::CustomControlMessage
);

use Carp ();

has minimum => (
    is        => 'ro',
    isa       => 'Int',
    predicate => 'has_minimum',
);

has maximum => (
    is        => 'ro',
    isa       => 'Int',
    predicate => 'has_maximum',
);

sub BUILDARGS {
    my $class = shift;
    my $args  = @_ == 1 ? $_[0] : { @_ };

    if (defined $args->{minimum} and defined $args->{maximum}
            and $args->{minimum} >= $args->{maximum}) {
        Carp::croak('length minimum must be less than maximum');
    }

    return $class->SUPER::BUILDARGS(@_);
}

sub check_control {
    my ($self, $control) = @_;

    return if $control->does('Form::Factory::Control::Role::ScalarValue');

    Carp::croak("the length feature only works with scalar values\n");
}

sub check {
    my $self  = shift;
    my $value = $self->control->current_value;

    return if length($value) == 0;

    if ($self->has_minimum and length($value) < $self->minimum) {
        $self->control_error(
            "the %s must be at least @{[$self->minimum]} characters long"
        );
        $self->result->is_valid(0);
    }

    if ($self->has_maximum and length($value) > $self->maximum) {
        $self->control_error(
            "the %s must be no longer than @{[$self->maximum]} characters"
        );
        $self->result->is_valid(0);
    }

    $self->result->is_valid(1) unless $self->result->is_validated;
}

1;