| 1 |
package HTML::FormHandler::Field::Float; |
| 2 |
# ABSTRACT: validate a float value |
| 3 |
$HTML::FormHandler::Field::Float::VERSION = '0.410002'; |
| 4 |
use HTML::FormHandler::Moose; |
| 5 |
extends 'HTML::FormHandler::Field::Text'; |
| 6 |
|
| 7 |
has '+size' => ( default => 8 ); |
| 8 |
has 'precision' => ( isa => 'Int|Undef', is => 'rw', default => 2 ); |
| 9 |
has 'decimal_symbol' => ( isa => 'Str', is => 'rw', default => '.'); |
| 10 |
has 'decimal_symbol_for_db' => ( isa => 'Str', is => 'rw', default => '.'); |
| 11 |
has '+inflate_method' => ( default => sub { \&inflate_float } ); |
| 12 |
has '+deflate_method' => ( default => sub { \&deflate_float } ); |
| 13 |
|
| 14 |
our $class_messages = { |
| 15 |
'float_needed' => 'Must be a number. May contain numbers, +, - and decimal separator \'[_1]\'', |
| 16 |
'float_size' => 'Total size of number must be less than or equal to [_1], but is [_2]', |
| 17 |
'float_precision' => 'May have a maximum of [quant,_1,digit] after the decimal point, but has [_2]', |
| 18 |
}; |
| 19 |
|
| 20 |
sub get_class_messages { |
| 21 |
my $self = shift; |
| 22 |
return { |
| 23 |
%{ $self->next::method }, |
| 24 |
%$class_messages, |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
sub inflate_float { |
| 29 |
my ( $self, $value ) = @_; |
| 30 |
return $value unless defined $value; |
| 31 |
$value =~ s/^\+//; |
| 32 |
return $value; |
| 33 |
} |
| 34 |
|
| 35 |
sub deflate_float { |
| 36 |
my ( $self, $value ) = @_; |
| 37 |
return $value unless defined $value; |
| 38 |
my $symbol = $self->decimal_symbol; |
| 39 |
my $symbol_db = $self->decimal_symbol_for_db; |
| 40 |
$value =~ s/\Q$symbol_db\E/$symbol/x; |
| 41 |
return $value; |
| 42 |
} |
| 43 |
|
| 44 |
sub validate { |
| 45 |
my $field = shift; |
| 46 |
|
| 47 |
#return unless $field->next::method; |
| 48 |
my ($integer_part, $decimal_part) = (); |
| 49 |
my $value = $field->value; |
| 50 |
my $symbol = $field->decimal_symbol; |
| 51 |
my $symbol_db = $field->decimal_symbol_for_db; |
| 52 |
|
| 53 |
if ($value =~ m/^-?([0-9]+)(\Q$symbol\E([0-9]+))?$/x) { # \Q ... \E - All the characters between the \Q and the \E are interpreted as literal characters. |
| 54 |
$integer_part = $1; |
| 55 |
$decimal_part = defined $3 ? $3 : ''; |
| 56 |
} |
| 57 |
else { |
| 58 |
return $field->add_error( $field->get_message('float_needed'), $symbol ); |
| 59 |
} |
| 60 |
|
| 61 |
if ( my $allowed_size = $field->size ) { |
| 62 |
my $total_size = length($integer_part) + length($decimal_part); |
| 63 |
return $field->add_error( $field->get_message('float_size'), |
| 64 |
$allowed_size, $total_size ) |
| 65 |
if $total_size > $allowed_size; |
| 66 |
} |
| 67 |
|
| 68 |
if ( my $allowed_precision = $field->precision ) { |
| 69 |
return $field->add_error( $field->get_message('float_precision'), |
| 70 |
$allowed_precision, length $decimal_part) |
| 71 |
if length $decimal_part > $allowed_precision; |
| 72 |
} |
| 73 |
|
| 74 |
# Inflate to database accepted format |
| 75 |
$value =~ s/\Q$symbol\E/$symbol_db/x; |
| 76 |
$field->_set_value($value); |
| 77 |
|
| 78 |
return 1; |
| 79 |
} |
| 80 |
|
| 81 |
__PACKAGE__->meta->make_immutable; |
| 82 |
use namespace::autoclean; |
| 83 |
1; |
| 84 |
|
| 85 |
__END__ |
| 86 |
|
| 87 |
=pod |
| 88 |
|
| 89 |
=encoding UTF-8 |
| 90 |
|
| 91 |
=head1 NAME |
| 92 |
|
| 93 |
HTML::FormHandler::Field::Float - validate a float value |
| 94 |
|
| 95 |
=head1 VERSION |
| 96 |
|
| 97 |
version 0.410002 |
| 98 |
|
| 99 |
=head1 DESCRIPTION |
| 100 |
|
| 101 |
This accepts a positive or negative float/integer. Negative numbers may |
| 102 |
be prefixed with a dash. By default a max of eight digits including 2 precision |
| 103 |
are accepted. Default decimal symbol is ','. |
| 104 |
Widget type is 'text'. |
| 105 |
|
| 106 |
# For example 1234,12 has size of 6 and precision of 2 |
| 107 |
# and separator symbol of ',' |
| 108 |
|
| 109 |
has_field 'test_result' => ( |
| 110 |
type => 'Float', |
| 111 |
size => 8, # Total size of number including decimal part. |
| 112 |
precision => 2, # Size of the part after decimal symbol. |
| 113 |
decimal_symbol => '.', # Decimal symbol accepted in web page form |
| 114 |
decimal_symbol_for_db => '.', # For inflation. Decimal symbol accepted in DB, which automatically converted to. |
| 115 |
range_start => 0, |
| 116 |
range_end => 100 |
| 117 |
); |
| 118 |
|
| 119 |
=head2 messages |
| 120 |
|
| 121 |
float_needed |
| 122 |
float_size |
| 123 |
float_precision |
| 124 |
|
| 125 |
=head1 AUTHOR |
| 126 |
|
| 127 |
FormHandler Contributors - see HTML::FormHandler |
| 128 |
|
| 129 |
=head1 COPYRIGHT AND LICENSE |
| 130 |
|
| 131 |
This software is copyright (c) 2017 by Gerda Shank. |
| 132 |
|
| 133 |
This is free software; you can redistribute it and/or modify it under |
| 134 |
the same terms as the Perl 5 programming language system itself. |
| 135 |
|
| 136 |
=cut |