Form::Sensible::Field::Number - A Numeric field type.
use Form::Sensible::Field::Number;
my $object = Form::Sensible::Field::Number->new(
integer_only => 1,
lower_bound => 10,
upper_bound => 100,
step => 5,
);
$object->do_stuff();
The number field type is one of the more advanced field types in Form::Sensible.
It has a number of features for dealing specifically with numbers. It can be
set to have a lower and upper bound, allowing validation to ensure that the
value selected is within a range. It can also be set to have a 'step', which
provides a constraint to what values are valid between the upper and lower
bounds. It can also be made to accept integers only, or fractional values.
Finally, it can be rendered in a number of ways including select
boxes, drop downs or even ranged-sliders if your renderer supports it.
- "integer_only"
- True/false value indicating whether this field is able to accept
fractional values. Required attribute.
- "lower_bound"
- Lower bound of valid values for the field.
- "upper_bound"
- Upper bound of valid values for the field.
- "step"
- When step is provided, the value provided must be a multiple of
"step" in order to be valid.
- "validate"
- Validates the field against the numeric constraints set for the
field.
- "get_potential_values($step, $lower_bound, $upper_bound)"
- Returns an array containing all the valid values between the upper and
lower bound. Used internally to the number field.
- "in_step($value, $step)"
- Returns true if $value lies on a
$step boundary. Otherwise returns false.
- "get_additional_configuration"
- Returns a hashref consisting of the attributes for this field and their
values.
The following two methods allow Number fields to be treated like
Select Fields for rendering purposes.
- "get_options"
- An array ref containing the allowed options. Each option is represented as
a hash containing a "name" element and a
"value" element for the given
option.
- "accepts_multiple"
- On a Select field, this defines whether the field can have multiple
values. For a Number field, only one value is allowed, so this always
returns false.
- "regex"
- The number field type by default checks that what was passed looks like a
number based on the following regex:
qr/^[-+]? # Sign
(?: [0-9]+ # Integer portion ...
(?: \. [0-9]* )? # Fractional portion
| \. [0-9]+ # Just a decimal
)
$/xms
This will handle most numbers you are likely to encounter.
However, if this regex is insufficient, such as when you need to process
numbers in exponential notation, you can provide a replacement regex in
the field's 'validation' hash:
my $object = Form::Sensible::Field::Number->new(
validation => {
# allow exponential notation
regex => qr/^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/,
}
);
Note that the Number validation routines outlined above are
not built to handle numbers perl can not handle natively. That is to
say, if you are working with numbers that perl can not parse, or that
require you to use modules such as Math::BigInt, you can still use the
Number class, but it's best to avoid Number's builtin validation. You
can perform your validation yourself in a
"code" validation block or by
subclassing Form::Sensible's Number or Text field classes.
Jay Kuri - <jayk@cpan.org>
Ionzero LLC. <http://ionzero.com/>
Copyright 2009 by Jay Kuri <jayk@cpan.org>
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.