Valiant::Validator::Date - Verify that a value is is a standard Date (YYY-MM-DD)
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 ] };
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.
This validator supports the following attributes:
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.
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)
If provided set an upper limit on the allowed date. Either a string in YYYY-MM-DD format or a DateTime object.
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... }
The error message / tag associated with the given validation failures. Default messages are provided.
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.
Returns a raw blessed DateTime object. If you pass a hash of arguments, those will be passed to new.
new
returns DateTime now
Return a DateTime object that is now plus or minus a given number of years.
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.
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 }
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', ... );
validates attribute => ( date => +{ min => sub { pop->is_future }, max => sub { pop->is_past } }, );
This validator supports all the standard shared parameters: if, unless, message, strict, allow_undef, allow_blank.
if
unless
message
strict
allow_undef
allow_blank
Valiant, Valiant::Validator, Valiant::Validator::Each.
See Valiant
1 POD Error
The following errors were encountered while parsing the POD:
Deleting unknown formatting code M<>
To install Valiant, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Valiant
CPAN shell
perl -MCPAN -e shell install Valiant
For more information on module installation, please visit the detailed CPAN module installation guide.