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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
package HTML::FormHandler::Field::Date;
# ABSTRACT: a date field with formats
$HTML::FormHandler::Field::Date::VERSION = '0.40067';
use Moose;
extends 'HTML::FormHandler::Field::Text';
use DateTime;
use DateTime::Format::Strptime;
has '+html5_type_attr' => ( default => 'date' );
has 'format' => ( is => 'rw', isa => 'Str', default => "%Y-%m-%d" );
has 'locale' => ( is => 'rw', isa => 'Str' ); # TODO
has 'time_zone' => ( is => 'rw', isa => 'Str' ); # TODO
has 'date_start' => ( is => 'rw', isa => 'Str', clearer => 'clear_date_start' );
has 'date_end' => ( is => 'rw', isa => 'Str', clearer => 'clear_date_end' );
has '+size' => ( default => '10' );
has '+deflate_method' => ( default => sub { \&date_deflate } );
# translator for Datepicker formats to DateTime strftime formats
my $dp_to_dt = {
"d" => "\%e", # day of month (no leading zero)
"dd" => "\%1", # day of month (2 digits) "%d"
"o" => "\%4", # day of year (no leading zero) "%{day_of_year}"
"oo" => "\%j", # day of year (3 digits)
"D" => "\%a", # day name long
"DD" => "\%A", # day name short
"m" => "\%5", # month of year (no leading zero) "%{day_of_month}"
"mm" => "\%3", # month of year (two digits) "%m"
"M" => "\%b", # Month name short
"MM" => "\%B", # Month name long
"y" => "\%2", # year (2 digits) "%y"
"yy" => "\%Y", # year (4 digits)
"@" => "\%s", # epoch
};
our $class_messages = {
'date_early' => 'Date is too early',
'date_late' => 'Date is too late',
};
sub get_class_messages {
my $self = shift;
return {
%{ $self->next::method },
%$class_messages,
}
}
sub date_deflate {
my ( $self, $value ) = @_;
# if not a DateTime, assume correctly formatted string and return
return $value unless blessed $value && $value->isa('DateTime');
my $format = $self->get_strf_format;
my $string = $value->strftime($format);
return $string;
}
sub validate {
my $self = shift;
my $format = $self->get_strf_format;
my @options;
push @options, ( time_zone => $self->time_zone ) if $self->time_zone;
push @options, ( locale => $self->locale ) if $self->locale;
my $strp = DateTime::Format::Strptime->new( pattern => $format, @options );
my $dt = eval { $strp->parse_datetime( $self->value ) };
unless ($dt) {
$self->add_error( $strp->errmsg || $@ );
return;
}
$self->_set_value($dt);
my $val_strp = DateTime::Format::Strptime->new( pattern => "%Y-%m-%d", @options );
if ( $self->date_start ) {
my $date_start = $val_strp->parse_datetime( $self->date_start );
die "date_start: " . $val_strp->errmsg unless $date_start;
my $cmp = DateTime->compare( $date_start, $dt );
$self->add_error($self->get_message('date_early')) if $cmp eq 1;
}
if ( $self->date_end ) {
my $date_end = $val_strp->parse_datetime( $self->date_end );
die "date_end: " . $val_strp->errmsg unless $date_end;
my $cmp = DateTime->compare( $date_end, $dt );
$self->add_error($self->get_message('date_late')) if $cmp eq -1;
}
}
sub get_strf_format {
my $self = shift;
# if contains %, then it's a strftime format
return $self->format if $self->format =~ /\%/;
my $format = $self->format;
foreach my $dpf ( reverse sort keys %{$dp_to_dt} ) {
my $strf = $dp_to_dt->{$dpf};
$format =~ s/$dpf/$strf/g;
}
$format =~ s/\%1/\%d/g,
$format =~ s/\%2/\%y/g,
$format =~ s/\%3/\%m/g,
$format =~ s/\%4/\%{day_of_year}/g,
$format =~ s/\%5/\%{day_of_month}/g,
return $format;
}
before 'get_tag' => sub {
my $self = shift;
if (
$self->form
&& $self->form->is_html5
&& $self->html5_type_attr eq 'date' # subclass may be using different input type
&& not( $self->format =~ /^(yy|%Y)-(mm|%m)-(dd|%d)$/ )
) {
warn "Form is HTML5, but date field '" . $self->full_name
. "' has a format other than %Y-%m-%d, which HTML5 requires for date "
. "fields. Either correct the date format or set the is_html5 flag to false.";
}
};
__PACKAGE__->meta->make_immutable;
use namespace::autoclean;
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
HTML::FormHandler::Field::Date - a date field with formats
=head1 VERSION
version 0.40067
=head1 SUMMARY
This field may be used with the jQuery Datepicker plugin.
You can specify the format for the date using jQuery formatDate strings
or DateTime strftime formats. (Default format is format => '%Y-%m-%d'.)
d - "%e" - day of month (no leading zero)
dd - "%d" - day of month (two digit)
o - "%{day_of_year}" - day of the year (no leading zeros)
oo - "%j" - day of the year (three digit)
D - "%a" - day name short
DD - "%A" - day name long
m - "%{day_of_month}" - month of year (no leading zero)
mm - "%m" - month of year (two digit) "%m"
M - "%b" - month name short
MM - "%B" - month name long
y - "%y" - year (two digit)
yy - "%Y" - year (four digit)
@ - "%s" - Unix timestamp (ms since 01/01/1970)
For example:
has_field 'start_date' => ( type => 'Date', format => "dd/mm/y" );
or
has_field 'start_date' => ( type => 'Date', format => "%d/%m/%y" );
You can also set 'date_end' and 'date_start' attributes for validation
of the date range. Use iso_8601 formats for these dates ("yyyy-mm-dd");
has_field 'start_date' => ( type => 'Date', date_start => "2009-12-25" );
Customize error messages 'date_early' and 'date_late':
has_field 'start_date' => ( type => 'Date,
messages => { date_early => 'Pick a later date',
date_late => 'Pick an earlier date', } );
=head2 Using with HTML5
If the field's form has its 'is_html5' flag active, then the field's rendering
behavior changes in two ways:
=over
=item *
It will render as <input type="date" ... /> instead of type="text".
=item *
If the field's format is set to anything other than ISO date format
(%Y-%m-%d), then attempting to render the field will result in a warning.
(Note that the default value for the field's format attribute is, in fact,
the ISO date format.)
=back
=head1 AUTHOR
FormHandler Contributors - see HTML::FormHandler
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2016 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
|