| 1 | package HTML::FormHandler::Field::DateTime; | ||||||||||
| 2 | # ABSTRACT: compound DateTime field | ||||||||||
| 3 | 7 | $HTML::FormHandler::Field::DateTime::VERSION = '0.410001'; | |||||||||
| 4 | 7 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 7 | extends 'HTML::FormHandler::Field::Compound'; | |||||||||
| 6 | |||||||||||
| 7 | 7 | ▶ | use DateTime; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 7 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | |||||||||||
| 11 | 7 | has '+widget' => ( default => 'Compound' ); | |||||||||
| 12 | 10 | ▶ | has '+inflate_default_method' => ( default => sub { \&datetime_inflate } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 7 | our $class_messages = { | |||||||||
| 15 | 'datetime_invalid' => 'Not a valid DateTime', | ||||||||||
| 16 | }; | ||||||||||
| 17 | sub get_class_messages { | ||||||||||
| 18 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get_class_messages: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 19 | return { | ||||||||||
| 20 | 1 | %{ $self->next::method }, | |||||||||
| 21 | %$class_messages, | ||||||||||
| 22 | } | ||||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | sub datetime_inflate { | ||||||||||
| 26 | 1 | ▶ | my ( $self, $value ) = @_; | ||||||||
|
Subroutine
datetime_inflate: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 27 | 1 | ▶ | return $value unless ref $value eq 'DateTime'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 1 | my %hash; | |||||||||
| 29 | 1 | foreach my $field ( $self->all_fields ) { | |||||||||
| 30 | 3 | my $meth = $field->name; | |||||||||
| 31 | 3 | $hash{$meth} = $value->$meth; | |||||||||
| 32 | } | ||||||||||
| 33 | 1 | return \%hash; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | sub validate { | ||||||||||
| 37 | 13 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
validate: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 38 | 13 | my @dt_parms; | |||||||||
| 39 | 13 | foreach my $child ( $self->all_fields ) { | |||||||||
| 40 | 39 | ▶ | next unless $child->value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 39 | push @dt_parms, ( $child->accessor => $child->value ); | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | # set the value | ||||||||||
| 45 | 13 | my $dt; | |||||||||
| 46 | try { | ||||||||||
| 47 | 13 | ▶ | $dt = DateTime->new(@dt_parms); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 48 | } | ||||||||||
| 49 | catch { | ||||||||||
| 50 | 1 | ▶ | $self->add_error( $self->get_message('datetime_invalid') ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 51 | 13 | }; | |||||||||
| 52 | 13 | ▶ | if( $dt ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 12 | $self->_set_value($dt); | |||||||||
| 54 | } | ||||||||||
| 55 | else { | ||||||||||
| 56 | 1 | $self->_set_value( {@dt_parms} ); | |||||||||
| 57 | } | ||||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 7 | __PACKAGE__->meta->make_immutable; | |||||||||
| 61 | 7 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 62 | 7 | 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.410001 | ||||||||||
| 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 | ||||||||||