| 1 | package Data::MuForm::Field::Integer; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: validate an integer value | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use Data::MuForm::Meta; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | extends 'Data::MuForm::Field::Text'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 8 | has '+html5_input_type' => ( default => 'number' ); | |||||||||||||||||||||||||||
| 9 | 8 | has '+size' => ( default => 8 ); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 8 | has 'range_start' => ( is => 'rw' ); | |||||||||||||||||||||||||||
| 12 | 8 | has 'range_end' => ( is => 'rw' ); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 8 | our $class_messages = { | |||||||||||||||||||||||||||
| 15 | 'integer_needed' => 'Value must be an integer', | ||||||||||||||||||||||||||||
| 16 | 'range_too_low' => 'Value must be greater than or equal to [_1]', | ||||||||||||||||||||||||||||
| 17 | 'range_too_high' => 'Value must be less than or equal to [_1]', | ||||||||||||||||||||||||||||
| 18 | 'range_incorrect' => 'Value must be between [_1] and [_2]', | ||||||||||||||||||||||||||||
| 19 | }; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub get_class_messages { | ||||||||||||||||||||||||||||
| 22 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_class_messages: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | return { | ||||||||||||||||||||||||||||
| 24 | 6 | %{ $self->next::method }, | |||||||||||||||||||||||||||
| 25 | %$class_messages, | ||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub validate { | ||||||||||||||||||||||||||||
| 30 | 19 | ▶ | my $field = shift; | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=11 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 19 | my $value = $field->value; | |||||||||||||||||||||||||||
| 33 | 19 | ▶ | return 1 unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 19 | $value =~ s/^\+//; | |||||||||||||||||||||||||||
| 36 | 19 | $field->value($value); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 19 | ▶ | unless ( $value =~ /^-?[0-9]+$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 2 | $field->add_error($field->get_message('integer_needed')); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 19 | my $low = $field->range_start; | |||||||||||||||||||||||||||
| 43 | 19 | my $high = $field->range_end; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 19 | ▶ | if ( defined $low && defined $high ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $low and defined $high
MC/DC100%2/2
defined $low and defined $high
defined $low
defined $high
Truth table100%3/3
A and B
A= defined $lowB= defined $high
| |||||||||||||||||||||||||||||
| 46 | return | ||||||||||||||||||||||||||||
| 47 | 11 | ▶ | $value >= $low && $value <= $high ? 1 : | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value >= $low and $value <= $high
MC/DC100%2/2
$value >= $low and $value <= $high
$value >= $low
$value <= $high
Truth table100%3/3
A and B
A= $value >= $lowB= $value <= $high
| |||||||||||||||||||||||||||||
| 48 | $field->add_error( $field->get_message('range_incorrect'), low => $low, high => $high ); | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 8 | ▶ | if ( defined $low ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | return | ||||||||||||||||||||||||||||
| 53 | 2 | ▶ | $value >= $low ? 1 : | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | $field->add_error( $field->get_message('range_too_low'), low => $low ); | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 6 | ▶ | if ( defined $high ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | return | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $value <= $high ? 1 : | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | $field->add_error( $field->get_message('range_too_high'), high => $high ); | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 6 | return 1; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 8 | 1; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | __END__ | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =pod | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 NAME | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Data::MuForm::Field::Integer - validate an integer value | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head1 VERSION | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | version 0.04 | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | This accepts a positive or negative integer. Negative integers may | ||||||||||||||||||||||||||||
| 87 | be prefixed with a dash. By default a max of eight digits are accepted. | ||||||||||||||||||||||||||||
| 88 | Widget type is 'text'. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | If form has 'is_html5' flag active it will render <input type="number" ... /> | ||||||||||||||||||||||||||||
| 91 | instead of type="text" | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | The 'range_start' and 'range_end' attributes may be used to limit valid numbers. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Gerda Shank | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | This software is copyright (c) 2017 by Gerda Shank. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 104 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||