1package Rose::HTML::Form::Field::Integer; 2 3use strict; 4 5use Rose::HTML::Object::Errors qw(:number); 6 7use base 'Rose::HTML::Form::Field::Numeric'; 8 9our $VERSION = '0.606'; 10 11sub validate 12{ 13 my($self) = shift; 14 15 my $ok = $self->SUPER::validate(@_); 16 return $ok unless($ok); 17 18 my $value = $self->internal_value; 19 return 1 unless(defined $value && length $value); 20 21 my $name = sub { $self->error_label || $self->name }; 22 23 unless($value =~ /^-?\d+$/) 24 { 25 $self->add_error_id(NUM_INVALID_INTEGER, { label => $name }); 26 return 0; 27 } 28 29 return 1; 30} 31 32my %Error_Map = 33( 34 NUM_INVALID_NUMBER() => NUM_INVALID_INTEGER, 35 NUM_INVALID_NUMBER_POSITIVE() => NUM_INVALID_INTEGER_POSITIVE, 36 NUM_NOT_POSITIVE_NUMBER() => NUM_NOT_POSITIVE_INTEGER, 37); 38 39sub add_error_id 40{ 41 my($self) = shift; 42 my $error_id = shift; 43 my $new_error_id = $Error_Map{$error_id} || $error_id; 44 return $self->SUPER::add_error_id($new_error_id, @_); 45} 46 47sub error_id 48{ 49 my($self) = shift; 50 51 if(@_) 52 { 53 my $error_id = shift; 54 my $new_error_id = $Error_Map{$error_id} || $error_id; 55 return $self->SUPER::error_id($new_error_id, @_); 56 } 57 else 58 { 59 my $error_id = $self->SUPER::error_id; 60 my $new_error_id = $Error_Map{$error_id} || $error_id; 61 return $new_error_id; 62 } 63} 64 65if(__PACKAGE__->localizer->auto_load_messages) 66{ 67 __PACKAGE__->localizer->load_all_messages; 68} 69 70use utf8; # The __DATA__ section contains UTF-8 text 71 721; 73 74__DATA__ 75 76[% LOCALE en %] 77 78NUM_INVALID_INTEGER = "[label] must be an integer." 79NUM_INVALID_INTEGER_POSITIVE = "[label] must be a positive integer." 80NUM_NOT_POSITIVE_INTEGER = "[label] must be a positive integer." 81 82[% LOCALE de %] 83 84NUM_INVALID_INTEGER = "[label] muß eine Ganzzahl sein." 85NUM_INVALID_INTEGER_POSITIVE = "[label] muß eine positive Ganzzahl sein." 86NUM_NOT_POSITIVE_INTEGER = "[label] muß eine positive Ganzzahl sein." 87 88[% LOCALE fr %] 89 90NUM_INVALID_INTEGER = "[label] doit être un entier." 91NUM_INVALID_INTEGER_POSITIVE = "[label] doit être un entier positif." 92NUM_NOT_POSITIVE_INTEGER = "[label] doit être un entier positif." 93 94[% LOCALE bg %] 95 96NUM_INVALID_INTEGER = "Полето '[label]' трябва да бъде цяло число." 97NUM_INVALID_INTEGER_POSITIVE = "Полето '[label]' трябва да бъде цяло положително число." 98NUM_NOT_POSITIVE_INTEGER = "Полето '[label]' трябва да бъде цяло положително число." 99 100__END__ 101 102=head1 NAME 103 104Rose::HTML::Form::Field::Integer - Text field that only accepts integer values. 105 106=head1 SYNOPSIS 107 108 $field = 109 Rose::HTML::Form::Field::Integer->new( 110 label => 'Count', 111 name => 'count', 112 maxlength => 6); 113 114 $field->input_value('abc'); 115 $field->validate; # false 116 117 $field->input_value(123); 118 $field->validate; # true 119 120 # Set minimum and maximum values 121 $field->min(2); 122 $field->max(100); 123 124 $field->input_value(123); 125 $field->validate; # false 126 127 $field->input_value(1); 128 $field->validate; # false 129 130 $field->input_value(5); 131 $field->validate; # true 132 133 print $field->html; 134 ... 135 136=head1 DESCRIPTION 137 138L<Rose::HTML::Form::Field::Integer> is a subclass of L<Rose::HTML::Form::Field::Numeric> that only accepts integer values. It overrides the L<validate()|Rose::HTML::Form::Field/validate> method of its parent class, returning true if the L<internal_value()|Rose::HTML::Form::Field/internal_value> is a valid integer, or setting an error message and returning false otherwise. 139 140Use the L<min|/min> and :<max|/max> attributes to control whether the range of valid values. 141 142=head1 OBJECT METHODS 143 144=over 4 145 146=item B<max [INT]> 147 148Get or set the maximum acceptable value. If the field's L<internal_value()|Rose::HTML::Form::Field/internal_value> is B<greater than> this value, then the L<validate()|Rose::HTML::Form::Field/validate> method will return false. If undefined, then no limit on the maximum value is enforced. 149 150=item B<min [INT]> 151 152Get or set the minimum acceptable value. If the field's L<internal_value()|Rose::HTML::Form::Field/internal_value> is B<less than> this value, then the L<validate()|Rose::HTML::Form::Field/validate> method will return false. If undefined, then no limit on the minimum value is enforced. 153 154=item B<negative [BOOL]> 155 156If BOOL is true or omitted, sets L<max|/max> to C<0>. If BOOL is false, sets L<max|/max> to undef. 157 158=item B<positive [BOOL]> 159 160If BOOL is true or omitted, sets L<min|/min> to C<0>. If BOOL is false, sets L<min|/min> to undef. 161 162=back 163 164=head1 AUTHOR 165 166John C. Siracusa (siracusa@gmail.com) 167 168=head1 LICENSE 169 170Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. 171