NAME

Valiant::Validator::Date - Verify that a value is is a standard Date (YYY-MM-DD)

SYNOPSIS

    package Local::Test::Date;

    use Moo;
    use Valiant::Validations;

    has birthday => (is=>'ro');

    validates birthday => (
      date => {
        min => sub { pop->years_ago(120) }, # Oldest person I think...
        max => sub { pop->now },
      }
    );

    my $object = Local::Test::Date->new(birthday=>'2100-01-01');
    $object->validate;

    warn $object->errors->_dump;

    $VAR1 = {
      'birthday' => [
         'chosen date can't be above {{max}}',  # In real life {{max}} would be
                                                # interpolated as DateTime->now
      ]
    };

DESCRIPTION

Validates a string pattern to make sure its in a standard date (YYYY-MM-DD) format, which is commonly used in databases as a Date field and its also the canonical pattern for the HTML5 input date type.

Can accept a 'min' and 'max' attribute, which should be either a string in the standard form or a DateTime object.

If you are using the Form helpers the max and min attributes can be reflected into the date input type automatically.

ATTRIBUTES

This validator supports the following attributes:

pattern

This is a string pattern that is used by DateTime::Format::Strptime that your date value must conform to (that is it must parse into a DateTime object or the validation fails). The default is '%Y-%m-%d'. This is a common database format and is also used by HTML5 input date type fields.

min

If provided set a bottom limit on the allowed date. Either a string in YYYY-MM-DD format or a DateTime object.

Value may also be a coderef so that you can set dynamic dates (such as always today)

max

If provided set an upper limit on the allowed date. Either a string in YYYY-MM-DD format or a DateTime object.

Value may also be a coderef so that you can set dynamic dates (such as always today)

cb

A code reference that lets you create custom validation logic. This is basically the same as the 'With' validator expect its only called IF the value is in valid date format and you get that date inflated into a DateTime object instead of the raw string value. This makes it a little less work for you since you can skip those extra checks. Also the coderef will receive the validator type instance as the third argument so that you can take advantage of the type helpers (see below \HELPERS).

    package MyRecord

    use Moo;
    use Valiant::Validations;

    has attribute => (is=>'ro');

    validates attribute => (
      date => +{ 
        min => sub { pop->years_ago(10) },
        max => sub { pop->now },
        cb => \&my_special_method,
      },
    );

    sub my_special_method {
      my ($self, $dt, $type) = @_;
      # In this case $dt is a DateTime object inflated from the value
      # of 'attribute'.  This method won't get called if we previously 
      # determine that the value isn't in proper YYY-MM-DD format.

      # Custom validation stuff...
    }

below_min_msg

above_max_msg

invalid_date_msg

The error message / tag associated with the given validation failures. Default messages are provided.

HELPERS

This validator provides the following helpers. These basically just wrap DateTime and DateTime::Format::Strptime so you can avoid having to create your own in your record / object classes.

datetime

Returns a raw blessed DateTime object. If you pass a hash of arguments, those will be passed to new.

now

returns DateTime now

years_ago

years_from_now

Return a DateTime object that is now plus or minus a given number of years.

is_future

is_past

Given a DateTime object (such as the value you are trying to validate), return true or false if it is either in the future or in the past.

SHORTCUT FORM

This validator supports the follow shortcut forms:

    validates attribute => ( date => 1, ... );

Which is the same as:

    validates attribute => (
      date => +{ },
    );

Not many saved characters but makes usage syntactically regular across validators.

You can also invoke a custom callback with a shortcut

    validates attribute => ( date => \&my_special_method, ... );

    sub my_special_method {
      my ($self, $dt, $type) = @_;
      # Custom validation stuff
    }

Which is the same as:

    validates attribute => (
      date => +{
        cb => \&my_special_method,
      },
    );

Lastly you can specify that the date must be either future or past with a shortcut:

    validates attribute => ( date => 'is_future', ... );
    validates attribute => ( date => 'is_past', ... );

Which is the same as:

    validates attribute => (
      date => +{
        min => sub { pop->is_future },
        max => sub { pop->is_past }
      },
    );

GLOBAL PARAMETERS

This validator supports all the standard shared parameters: if, unless, message, strict, allow_undef, allow_blank.

SEE ALSO

Valiant, Valiant::Validator, Valiant::Validator::Each.

AUTHOR

See Valiant

COPYRIGHT & LICENSE

See Valiant

1 POD Error

The following errors were encountered while parsing the POD:

Around line 154:

Deleting unknown formatting code M<>