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