[html-formfu] HTML::FormFu::Constraint::DateTimeRange - comments requested

q quarky at quantentunnel.de
Tue Jan 20 11:23:29 GMT 2009


Hi everybody,

the other day I had a problem with a text field that contains a date value, 
that I wanted to be constraint to a specific date range.

Now I came up with this solution and would like to receive any comments or 
critics for this. I am not a very experienced perl programmer, so I hacked 
this together from the HTML::FormFu::Constraint::Range and some other 
modules.

Best Regards

Wolfgang



___

package HTML::FormFu::Constraint::DateTimeRange;

use strict;

use base 'HTML::FormFu::Constraint';

use DateTime::Format::Natural;

use Scalar::Util qw( looks_like_number );

__PACKAGE__->mk_item_accessors( qw( strptime minimum maximum natural_min 
natural_max ) );

*strptime = \&strptime;

*min = \&minimum;

*max = \&maximum;

__PACKAGE__->mk_item_accessors( qw( _builder ) );

sub new {

my $self = shift->next::method(@_);

$self->_builder( DateTime::Format::Builder->new );

return $self;

}

sub parser {

my $self = shift;

$self->_builder->parser(@_);

return $self;

}

sub clone {

my $self = shift;

my $clone = $self->next::method(@_);

$clone->_builder( $self->_builder->clone );

return $clone;

}

sub constrain_value {

my ( $self, $value ) = @_;

return 1 if !defined $value || $value eq '';

my $dt = $self->_builder->parse_datetime($value);

if ( defined ( my $min = $self->minimum ) ) {

my $dt_min = $self->_builder->parse_datetime($min);

return 0 if ( $dt->epoch < $dt_min->epoch );

}

if ( defined ( my $max = $self->maximum ) ) {

my $dt_max = $self->_builder->parse_datetime($max);

return 0 if ( $dt->epoch > $dt_max->epoch );

}

if ( defined ( my $min = $self->natural_min ) ) {

my $parser = DateTime::Format::Natural->new;

my $dt_min = $parser->parse_datetime($min);

return 0 if ( $dt->epoch < $dt_min->epoch );

}

if ( defined ( my $max = $self->natural_max ) ) {

my $parser = DateTime::Format::Natural->new;

my $dt_max = $parser->parse_datetime($max);

return 0 if $dt->epoch > $dt_max->epoch

}

return 1;

}

sub _localize_args {

my ($self) = @_;

return $self->min, $self->max;

}

1;

__END__

=head1 NAME

HTML::FormFu::Constraint::DateTimeRange - DateTime Range Constraint

=head1 SYNOPSIS

type: DateTimeRange

parser:

strptime: '%Y-%m-%d'

min: 2009-01-01

max: 2010-01-01

or

type: DateTimeRange

parser:

strptime: '%Y-%m-%d'

natural_min: yesterday

natural_max: 3 months from now



=head1 DESCRIPTION

DateTime range constraint.

This constraint doesn't honour the C<not()> value.

=head1 METHODS

=head2 parser

Arguments: \%args

Required. Format used to turn the entered field value, the

min value and the max value into a L<DateTime> object.

Accepts arguments to be passed to L<DateTime::Format::Builder/parser>.

=head2 minimum

=head2 min

If defined, the input value must be equal to or greater than this.

The specified values are passed to the parser object specified above.

L</min> is an alias for L</minimum>.

=head2 maximum

=head2 max

If defined, the input value must be equal to or less than this.

The specified values are passed to the parser object specified above.

L</max> is an alias for L</maximum>.

=head2 natural_min

=head2 natural_max

Same as min and max above, but the parsing on the values is done

with a L<DateTime::Format::Natural/parser>.

=head1 SEE ALSO

Is a sub-class of, and inherits methods from L<HTML::FormFu::Constraint>

L<HTML::FormFu>

=head1 AUTHOR

Carl Franks C<cfranks at cpan.org>

=head1 LICENSE

This library is free software, you can redistribute it and/or modify it 
under

the same terms as Perl itself.




More information about the HTML-FormFu mailing list