Subject: | Warnings and incorrect logic |
use strict;
use warnings;
use Test::Most;
use Form::Sensible::Form;
use Form::Sensible::Field::Number;
my $form = Form::Sensible::Form->new;
my $field = Form::Sensible::Field::Number->new(
name => 'number',
integer_only => 1,
lower_bound => 10,
upper_bound => 100,
);
isa_ok( $field, 'Form::Sensible::Field::Number');
$form->add_field( $field );
$field->value('wheat');
is(
scalar keys %{$form->validate->error_fields},
1,
'NaN'
);
done_testing();
__END__
OUTPUTS:
k 1 - The object isa Form::Sensible::Field::Number
Argument "wheat" isn't numeric in numeric lt (<) at
/opt/local/lib/perl5/site_perl/5.8.9/Form/Sensible/Field/Number.pm line 38.
Argument "wheat" isn't numeric in numeric gt (>) at
/opt/local/lib/perl5/site_perl/5.8.9/Form/Sensible/Field/Number.pm line 41.
ok 2 - NaN
CORRECTION to Number.pm:
ORIG:
33 around 'validate' => sub {
34 my $orig = shift;
35 my $self = shift;
36
37 my @errors;
38 if (defined($self->lower_bound) && $self->value <
$self->lower_bound) {
39 push @errors, "_FIELDNAME_ is lower than the minimum allowed
value";
40 }
41 if (defined($self->upper_bound) && $self->value >
$self->upper_bound) {
42 push @errors, "_FIELDNAME_ is higher than the maximum
allowed value";
43 }
44
45 if ( $self->integer_only ){
46 if( $self->value !~ /^-?\d+(\.d+)?$/ ){
47 push @errors, "_FIELDNAME_ must be a number";
48 } elsif ( $self->value != int($self->value)) {
49 push @errors, "_FIELDNAME_ must be an integer.";
50 }
51 }
FIX:
around 'validate' => sub {
my $orig = shift;
my $self = shift;
my @errors;
# Navie check for it being a simple number
# - to handle exponential/sci notation, I use Math::Big*
if( $self->value !~ /^-?\d+(\.d+)?$/ ){
push @errors, "_FIELDNAME_ must be a number";
}
# Won't handle bigint (Math::BigInt will)
elsif ( $self->integer_only
and $self->value != int($self->value)) {
push @errors, "_FIELDNAME_ must be an integer.";
}
# Only check bounds if a valid number
else {
if (defined($self->lower_bound) && $self->value < $self->lower_bound) {
push @errors, "_FIELDNAME_ is lower than the minimum allowed value";
}
if (defined($self->upper_bound) && $self->value > $self->upper_bound) {
push @errors, "_FIELDNAME_ is higher than the maximum allowed value";
}
}