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