1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
package HTML::FormHandler::Field::Duration;
# ABSTRACT: DateTime::Duration from HTML form values
use Moose;
extends 'HTML::FormHandler::Field::Compound';
use DateTime;
our $VERSION = '0.01';
our $class_messages = {
'duration_invalid' => 'Invalid value for [_1]: [_2]',
};
sub get_class_messages {
my $self = shift;
return {
%{ $self->next::method },
%$class_messages,
}
}
sub validate {
my ($self) = @_;
my @dur_parms;
foreach my $child ( $self->all_fields ) {
unless ( $child->has_value && $child->value =~ /^\d+$/ ) {
$self->add_error( $self->get_message('duration_invalid'), $self->loc_label, $child->loc_label );
next;
}
push @dur_parms, ( $child->accessor => $child->value );
}
# set the value
my $duration = DateTime::Duration->new(@dur_parms);
$self->_set_value($duration);
}
__PACKAGE__->meta->make_immutable;
use namespace::autoclean;
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
HTML::FormHandler::Field::Duration - DateTime::Duration from HTML form values
=head1 VERSION
version 0.40057
=head1 SubFields
Subfield names:
years, months, weeks, days, hours, minutes, seconds, nanoseconds
For example:
has_field 'duration' => ( type => 'Duration' );
has_field 'duration.hours' => ( type => 'Hour' );
has_field 'duration.minutes' => ( type => 'Minute' );
Customize error message 'duration_invalid' (default 'Invalid value for [_1]: [_2]')
=head1 AUTHOR
FormHandler Contributors - see HTML::FormHandler
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Gerda Shank.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|