Form::Processor::Field::DateTimeDMYHM - DEPRECATED example of a compound field


Form-Processor documentation Contained in the Form-Processor distribution.

Index


Code Index:

NAME

Top

Form::Processor::Field::DateTimeDMYHM - DEPRECATED example of a compound field

SYNOPSIS

Top

See Form::Processor

DESCRIPTION

Top

This is a compound field that uses modified field names for the sub fields instead of using a separate sub-form.

This is not well tested and should only be used after extensive testing. It's more of an example than a real field.

Widget

Fields can be given a widget type that is used as a hint for the code that renders the field.

This field's widget type is: "Compound".

Subclass

Fields may inherit from other fields. This field inherits from: "Field".

DEPENDENCIES

Top

DateTime

AUTHORS

Top

Bill Moseley

COPYRIGHT

Top

SUPPORT / WARRANTY

Top

Form::Processor is free software and is provided WITHOUT WARRANTY OF ANY KIND. Users are expected to review software for fitness and usability.


Form-Processor documentation Contained in the Form-Processor distribution.

package Form::Processor::Field::DateTimeDMYHM;
use strict;
use warnings;
use base 'Form::Processor::Field';
use DateTime;
our $VERSION = '0.03';

sub init_widget { 'Compound' }



# override completely validate

sub validate_field {
    my ( $self ) = @_;

    my $params = $self->form->params;

    my $name = $self->name;

    my %date;

    my $found = 0;

    for my $sub ( qw/ month day year hour minute / ) {
        my $value = $params->{ $name . '.' . $sub };

        next unless defined $value;
        $found++;

        unless ( $value =~ /^\d+$/ ) {
            $self->add_error( "Invalid value for '[_1]", $sub );
            return;
        }
        $date{ $sub } = $value;
    }

    # If any found, make sure all are entered
    if ( $self->required ) {
        unless ( $found ) {
            $self->add_error( "Date is required" );
            return;
        }
    }


    my $dt;
    eval {  $dt = DateTime->new( %date, time_zone => 'floating' ) };

    if ( $@ ) {
        my $error = $@;
        $error =~ s! at .+$/!!;
        # probably don't want to use that error message directly
        $self->add_error( "Invalid date ([_1])", "$error" );
        return;
    }

    $self->value( $dt );

    1;
}

sub format_value {
    my $self = shift;


    my $name = $self->name;

    my %hash;

    my $dt = $self->value || return ();


    for my $sub ( qw/ month day year hour minute / ) {

        $hash{ $name . '.' . $sub } = sprintf( '%02d', $dt->$sub );
    }

    return %hash;
}




1;