[Catalyst] How to use HTML::Widget::Constraint::Date

Alejandro Imass alejandro.imass at gmail.com
Sun Nov 26 20:46:06 GMT 2006


Hi Carl!

Thank you again for such a wonderful idea!

I decided to create my own class in my controller namespace but I
think it can be contributed to HTML::Widget::Constraint namespace if
anyone here has acces to that. Here is the class code just in case
anyone here wants to use it or forward it to the HTML::Widget people.
I am really beaten up by this project I am working on so I can't time
at the momento to contrib with a formal patch... bewarned though: it's
a quick an dirty patch to solve my problem and it works only for ISO
type date (yyyy-mm-dd).

To anyone interested in using it just create it in your controller
namespace and ajust the package name accordingly. Then in your code
just do something like:

$w->constraint("+vdc::Controller::DateField" => qw/ fecha_nacimiento
fecha_muerte /) ->message('Formato: AAAA-MM-DD');


package vdc::Controller::DateField;

use warnings;
use strict;
use base 'HTML::Widget::Constraint';
use Date::Calc;

=head1 NAME

 vdc::Controller::DateField - Date Field Constraint (in just one field)

=head1 SYNOPSIS

    my $c = $widget->constraint( 'DateField', @field_names );

=head1 DESCRIPTION

Much like 'Date' Constraint but in just one field.
It ONLY supports ISO format yyyy-mm-dd. But it's
easier to deal with if you just have single date fields.

=head1 METHODS

=head2 process

=cut

sub process {
    my ( $self, $w, $params ) = @_;

    return []
        unless ( $self->names && @{ $self->names } > 0 );

    my @names = @{ $self->names };

    my $results = [];

    foreach my $name (@names){

	my ($y,$m,$d) = split /\-/,$params->{$name};

	unless ( $y =~ /^\d+$/
		 && $m =~ /^\d+$/
		 && $d =~ /^\d+$/
		 && Date::Calc::check_date( $y, $m, $d ) )
	{
	    push @$results, HTML::Widget::Error->new(
						     { name => $name, message => $self->mk_message } );
	}
	
    }

    return $results;
}

=head1 AUTHOR

DateField Constraint by Alejandro Imass <ait at p2ee.org>

Hacked directly from Original Date by Sebastian Riedel, C<sri at oook.de>

=head1 LICENSE

This library is free software, you can redistribute it and/or modify it under
the same terms as Perl itself.

=cut

1;


On 11/26/06, Carl Franks <fireartist at gmail.com> wrote:
> On 26/11/06, Alejandro Imass <alejandro.imass at gmail.com> wrote:
> > Thanks!
> >
> > But how do I use this constraint if I have several date fields in my
> > form? Suppose I have two dates which would mean 6 fields on the form
> > (per your explanation above). Do I just pass all 6 in order to the
> > constraint? or am I missing the point entirely?
>
> You would add 2 constraints - something like:
>
> $form->constraint( Date => 'start_year', 'start_month', 'start_day' );
> $form->constraint( Date => 'end_year', 'end_month', 'end_day' );
>
> If your data already has the date in a single field, rather than 3,
> create a Callback constraint, and base the code on the check in
> HTML/Widget/Constraint/Date.pm
>
> Carl
>
> _______________________________________________
> List: Catalyst at lists.rawmode.org
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
>



More information about the Catalyst mailing list