| 1 | package HTML::Widget::Constraint::DateTime; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use base 'HTML::Widget::Constraint'; | ||
| 6 | use Date::Calc; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | HTML::Widget::Constraint::DateTime - DateTime Constraint | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | my $c = | ||
| 15 | $widget->constraint( 'DateTime', 'year', 'month', 'day', 'hour', | ||
| 16 | 'minute', 'second' ); | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | DateTime Constraint. | ||
| 21 | |||
| 22 | =head1 METHODS | ||
| 23 | |||
| 24 | =head2 process | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | sub process { | ||
| 29 | my ( $self, $w, $params ) = @_; | ||
| 30 | |||
| 31 | # PS: Commented out. What if I don't want to provide time? I could use | ||
| 32 | # 'Date', but, all these fields are allowed to be 0 anyway. And I may | ||
| 33 | # not want to check seconds or minutes. So we'll change this to #3 | ||
| 34 | |||
| 35 | # return [] | ||
| 36 | # unless ( $self->names && @{ $self->names } == 6 ); | ||
| 37 | return [] unless ( $self->names && @{ $self->names } >= 3 ); | ||
| 38 | |||
| 39 | my ( $year, $month, $day, $hour, $min, $sec ) = @{ $self->names }; | ||
| 40 | my $y = $params | ||
| 41 | ->{$year}; # 0 is a valid year, but Date::Calc doesn't support it | ||
| 42 | my $mo = $params->{$month}; | ||
| 43 | my $d = $params->{$day}; | ||
| 44 | my $h = $params->{$hour} || 0; | ||
| 45 | my $mi = $params->{$min} || 0; | ||
| 46 | my $s = $params->{$sec} || 0; | ||
| 47 | |||
| 48 | # PS: Commented out. This is silly. Hour, minute, and second | ||
| 49 | # can all be validly 0. | ||
| 50 | # return [] unless ( $y && $mo && $d && $h && $mi && $s ); | ||
| 51 | return [] unless ( $y && $mo && $d ); | ||
| 52 | |||
| 53 | my $results = []; | ||
| 54 | |||
| 55 | unless ( $y =~ /^\d+$/ | ||
| 56 | && $mo =~ /^\d+$/ | ||
| 57 | && $d =~ /^\d+$/ | ||
| 58 | && $h =~ /^\d+$/ | ||
| 59 | && $mi =~ /^\d+$/ | ||
| 60 | && $s =~ /^\d+$/ | ||
| 61 | && Date::Calc::check_date( $y, $mo, $d ) | ||
| 62 | && Date::Calc::check_time( $h, $mi, $s ) ) | ||
| 63 | { | ||
| 64 | push @$results, HTML::Widget::Error->new( | ||
| 65 | { name => $year, message => $self->mk_message } ); | ||
| 66 | push @$results, HTML::Widget::Error->new( | ||
| 67 | { name => $month, message => $self->mk_message } ); | ||
| 68 | push @$results, HTML::Widget::Error->new( | ||
| 69 | { name => $day, message => $self->mk_message } ); | ||
| 70 | push @$results, HTML::Widget::Error->new( | ||
| 71 | { name => $hour, message => $self->mk_message } ); | ||
| 72 | push @$results, HTML::Widget::Error->new( | ||
| 73 | { name => $min, message => $self->mk_message } ); | ||
| 74 | push @$results, HTML::Widget::Error->new( | ||
| 75 | { name => $sec, message => $self->mk_message } ); | ||
| 76 | } | ||
| 77 | return $results; | ||
| 78 | } | ||
| 79 | |||
| 80 | =head1 AUTHOR | ||
| 81 | |||
| 82 | Sebastian Riedel, C<sri@oook.de> | ||
| 83 | |||
| 84 | =head1 LICENSE | ||
| 85 | |||
| 86 | This library is free software, you can redistribute it and/or modify it under | ||
| 87 | the same terms as Perl itself. | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | 1; |