Debian Codegraph @

lib/HTML/FormHandler/Field/Money.pm

raw
1 package HTML::FormHandler::Field::Money;
2 # ABSTRACT: US currency-like values
3 $HTML::FormHandler::Field::Money::VERSION = '0.410002';
4 use HTML::FormHandler::Moose;
5 extends 'HTML::FormHandler::Field::Text';
6
7 has '+html5_type_attr' => ( default => 'number' );
8 has 'currency_symbol' => (
9 is => 'ro',
10 isa => 'Str',
11 default => '$',
12 );
13 has 'allow_commas' => (
14 is => 'ro',
15 isa => 'Bool',
16 default => 0,
17 );
18
19 our $class_messages = {
20 'money_convert' => 'Value cannot be converted to money',
21 'money_real' => 'Value must be a real number',
22 };
23
24 sub get_class_messages {
25 my $self = shift;
26 return {
27 %{ $self->next::method },
28 %$class_messages,
29 }
30 }
31
32 apply(
33 [ { # remove any leading currency symbol
34 transform => sub {
35 my ( $value, $field ) = @_;
36 my $c = $field->currency_symbol;
37 $value =~ s/^\Q$c\E// if $c;
38 return $value;
39 },
40 },
41 { # check number looks real, optionally allow comma digit groupings
42 check => sub {
43 my ( $value, $field ) = @_;
44 return $field->allow_commas
45 ? $value =~ /^[-+]?(?:\d+|\d{1,3}(,\d{3})*)(?:\.\d+)?$/
46 : $value =~ /^[-+]?\d+(?:\.\d+)?$/;
47 },
48 message => sub {
49 my ( $value, $field ) = @_;
50 return [ $field->get_message('money_real'), $value ];
51 },
52 },
53 { # remove commas
54 transform => sub {
55 my ($value, $field) = @_;
56 $value =~ tr/,//d if $field->allow_commas;
57 return $value;
58 },
59 },
60 { # convert to standard number, formatted to 2 decimal palaces
61 transform => sub { sprintf '%.2f', $_[0] },
62 message => sub {
63 my ( $value, $field ) = @_;
64 return [ $field->get_message('money_convert'), $value ];
65 },
66 },
67 ]
68 );
69
70
71
72 __PACKAGE__->meta->make_immutable;
73 use namespace::autoclean;
74 1;
75
76 __END__
77
78 =pod
79
80 =encoding UTF-8
81
82 =head1 NAME
83
84 HTML::FormHandler::Field::Money - US currency-like values
85
86 =head1 VERSION
87
88 version 0.410002
89
90 =head1 DESCRIPTION
91
92 Validates that a positive or negative real value is entered.
93 Formatted with two decimal places.
94
95 Uses a period for the decimal point. Widget type is 'text'.
96
97 If form has 'is_html5' flag active it will render <input type="number" ... />
98 instead of type="text"
99
100 =head1 ATTRIBUTES
101
102 =head2
103
104 =over
105
106 =item currency_symbol
107
108 Currency symbol to remove from start of input if found, default is dollar
109 C<$>.
110
111 =item allow_commas
112
113 Allow commas in input for digit grouping? Digits are grouped into groups of 3,
114 for example C<1,000,000,000>. Defaults to I<false>.
115
116 =back
117
118 =head1 AUTHOR
119
120 FormHandler Contributors - see HTML::FormHandler
121
122 =head1 COPYRIGHT AND LICENSE
123
124 This software is copyright (c) 2017 by Gerda Shank.
125
126 This is free software; you can redistribute it and/or modify it under
127 the same terms as the Perl 5 programming language system itself.
128
129 =cut