| 1 |
package HTML::FormHandler::Field::DateTime; |
| 2 |
# ABSTRACT: compound DateTime field |
| 3 |
$HTML::FormHandler::Field::DateTime::VERSION = '0.410002'; |
| 4 |
use Moose; |
| 5 |
extends 'HTML::FormHandler::Field::Compound'; |
| 6 |
|
| 7 |
use DateTime; |
| 8 |
use Try::Tiny; |
| 9 |
|
| 10 |
|
| 11 |
has '+widget' => ( default => 'Compound' ); |
| 12 |
has '+inflate_default_method' => ( default => sub { \&datetime_inflate } ); |
| 13 |
|
| 14 |
our $class_messages = { |
| 15 |
'datetime_invalid' => 'Not a valid DateTime', |
| 16 |
}; |
| 17 |
sub get_class_messages { |
| 18 |
my $self = shift; |
| 19 |
return { |
| 20 |
%{ $self->next::method }, |
| 21 |
%$class_messages, |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
sub datetime_inflate { |
| 26 |
my ( $self, $value ) = @_; |
| 27 |
return $value unless ref $value eq 'DateTime'; |
| 28 |
my %hash; |
| 29 |
foreach my $field ( $self->all_fields ) { |
| 30 |
my $meth = $field->name; |
| 31 |
$hash{$meth} = $value->$meth; |
| 32 |
} |
| 33 |
return \%hash; |
| 34 |
} |
| 35 |
|
| 36 |
sub validate { |
| 37 |
my ($self) = @_; |
| 38 |
my @dt_parms; |
| 39 |
foreach my $child ( $self->all_fields ) { |
| 40 |
next unless $child->value; |
| 41 |
push @dt_parms, ( $child->accessor => $child->value ); |
| 42 |
} |
| 43 |
|
| 44 |
# set the value |
| 45 |
my $dt; |
| 46 |
try { |
| 47 |
$dt = DateTime->new(@dt_parms); |
| 48 |
} |
| 49 |
catch { |
| 50 |
$self->add_error( $self->get_message('datetime_invalid') ); |
| 51 |
}; |
| 52 |
if( $dt ) { |
| 53 |
$self->_set_value($dt); |
| 54 |
} |
| 55 |
else { |
| 56 |
$self->_set_value( {@dt_parms} ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
__PACKAGE__->meta->make_immutable; |
| 61 |
use namespace::autoclean; |
| 62 |
1; |
| 63 |
|
| 64 |
__END__ |
| 65 |
|
| 66 |
=pod |
| 67 |
|
| 68 |
=encoding UTF-8 |
| 69 |
|
| 70 |
=head1 NAME |
| 71 |
|
| 72 |
HTML::FormHandler::Field::DateTime - compound DateTime field |
| 73 |
|
| 74 |
=head1 VERSION |
| 75 |
|
| 76 |
version 0.410002 |
| 77 |
|
| 78 |
=head1 DESCRIPTION |
| 79 |
|
| 80 |
This is a compound field that requires you to define the subfields |
| 81 |
for month/day/year/hour/minute. Widget type is 'compound'. |
| 82 |
|
| 83 |
If you want to use drop-down select boxes for your DateTime, you |
| 84 |
can select fields like: |
| 85 |
|
| 86 |
has_field 'my_date' => ( type => 'DateTime' ); |
| 87 |
has_field 'my_date.month' => ( type => 'Month' ); |
| 88 |
has_field 'my_date.day' => ( type => 'MonthDay' ); |
| 89 |
has_field 'my_date.year' => ( type => 'Year' ); |
| 90 |
has_field 'my_date.hour' => ( type => 'Hour' ); |
| 91 |
has_field 'my_date.minute' => ( type => 'Minute' ); |
| 92 |
|
| 93 |
If you want simple input fields: |
| 94 |
|
| 95 |
has_field 'my_date' => ( type => 'DateTime' ); |
| 96 |
has_field 'my_date.month' => ( type => 'Integer', range_start => 1, |
| 97 |
range_end => 12 ); |
| 98 |
has_field 'my_date.day' => ( type => 'Integer', range_start => 1, |
| 99 |
range_end => 31 ); |
| 100 |
|
| 101 |
Customizable error: 'datetime_invalid' (default = "Not a valid DateTime") |
| 102 |
|
| 103 |
See the 'Date' field for a single input date field. |
| 104 |
|
| 105 |
=head1 AUTHOR |
| 106 |
|
| 107 |
FormHandler Contributors - see HTML::FormHandler |
| 108 |
|
| 109 |
=head1 COPYRIGHT AND LICENSE |
| 110 |
|
| 111 |
This software is copyright (c) 2017 by Gerda Shank. |
| 112 |
|
| 113 |
This is free software; you can redistribute it and/or modify it under |
| 114 |
the same terms as the Perl 5 programming language system itself. |
| 115 |
|
| 116 |
=cut |