| 1 | package Valiant::Validator::Date; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use Valiant::I18N; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use DateTime::Format::Strptime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 7 | our $_pattern = '%Y-%m-%d'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 7 | with 'Valiant::Validator::Each'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 7 | has min => (is=>'ro', required=>0, predicate=>'has_min'); | |||||||||||||||||||||||||||
| 14 | 7 | has max => (is=>'ro', required=>0, predicate=>'has_max'); | |||||||||||||||||||||||||||
| 15 | 7 | has cb => (is=>'ro', required=>0, predicate=>'has_cb'); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 7 | ▶ | has pattern => (is=>'ro', required=>1, default=>sub { $_pattern }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 6 | ▶ | has _strp => (is=>'ro', required=>1, lazy=>1, default=>sub { DateTime::Format::Strptime->new(pattern => shift->pattern) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 7 | ▶ | has below_min_msg => (is=>'ro', required=>1, default=>sub {_t 'below_min'}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 7 | ▶ | has above_max_msg => (is=>'ro', required=>1, default=>sub {_t 'above_max'}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 7 | ▶ | has invalid_date_msg => (is=>'ro', required=>1, default=>sub {_t 'invalid_date'}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub normalize_shortcut { | ||||||||||||||||||||||||||||
| 26 | 2 | ▶ | my ($class, $arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
normalize_shortcut: called CC=6 SCAR=22.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | return +{ } if $arg eq '1'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | return +{ cb => $arg } if ((ref($arg)||'') eq 'CODE'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $arg || ''
MC/DC0%0/1
ref $arg || ''
ref $arg
Truth table50%1/2
A
A= ref $arg
| |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | return +{ min => sub { pop->now } } if $arg eq 'is_future'; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | return +{ max => sub { pop->now } } if $arg eq 'is_past'; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub validate_each { | ||||||||||||||||||||||||||||
| 35 | 50 | ▶ | my ($self, $record, $attribute, $value, $opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate_each: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 50 | ▶ | unless(defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 4 | $record->errors->add($attribute, $self->invalid_date_msg, $opts); | |||||||||||||||||||||||||||
| 39 | 4 | return; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 46 | my $dt = $self->_strp->parse_datetime($value); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 46 | ▶ | unless($dt) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 1 | $record->errors->add($attribute, $self->invalid_date_msg, $opts); | |||||||||||||||||||||||||||
| 46 | 1 | return; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 45 | ▶ | if($self->has_min) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 45 | my $min = $self->_cb_value($record, $self->min); | |||||||||||||||||||||||||||
| 51 | 45 | my $min_dt_obj = $self->parse_if_needed($min); | |||||||||||||||||||||||||||
| 52 | 45 | ▶ | $record->errors->add($attribute, $self->below_min_msg, +{%$opts, min=>$min_dt_obj->strftime($self->pattern)}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | unless $dt > $min_dt_obj; | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 45 | ▶ | if($self->has_max) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 41 | my $max = $self->_cb_value($record, $self->max); | |||||||||||||||||||||||||||
| 58 | 41 | my $max_dt_obj = $self->parse_if_needed($max); | |||||||||||||||||||||||||||
| 59 | 41 | ▶ | $record->errors->add($attribute, $self->above_max_msg, +{%$opts, max=>$max_dt_obj->strftime($self->pattern)}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | unless $dt < $max_dt_obj; | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 45 | ▶ | if($self->has_cb) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 4 | $self->cb->($record, $attribute, $dt, $self, $opts); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub to_pattern { | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | my ($self, $dt) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_pattern: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 0 | return $dt->strftime($self->pattern); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub parse_if_needed { | ||||||||||||||||||||||||||||
| 74 | 86 | ▶ | my ($self, $value_proto) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_if_needed: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 86 | ▶ | return $value_proto if blessed($value_proto) && $value_proto->isa('DateTime'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($value_proto) and $value_proto->isa("DateTime")
MC/DC0%0/2
builtin::blessed($value_proto) and $value_proto->isa("DateTime")
builtin::blessed($value_proto)
$value_proto->isa("DateTime")
Truth table33%1/3
A and B
A= builtin::blessed($value_proto)B= $value_proto->isa("DateTime")
| |||||||||||||||||||||||||||||
| 76 | 0 | return $self->_strp->parse_datetime($value_proto); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub looks_like_a_date { | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my ($self, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
looks_like_a_date: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 0 | my $dt = $self->parse_if_needed($value); | |||||||||||||||||||||||||||
| 82 | 0 | return $dt; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | sub is_future { | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my ($self, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_future: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 0 | my $dt = $self->parse_if_needed($value); | |||||||||||||||||||||||||||
| 88 | 0 | return $dt > DateTime->now; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub is_past { | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | my ($self, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_past: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 0 | my $dt = $self->parse_if_needed($value); | |||||||||||||||||||||||||||
| 94 | 0 | return $dt < DateTime->now; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 45 | ▶ | sub now { DateTime->now } | ||||||||||||||||||||||||||
|
Subroutine
now: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub datetime { | ||||||||||||||||||||||||||||
| 100 | 4 | ▶ | my($self, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
datetime: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 4 | return DateTime->new(%args); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | sub years_ago { | ||||||||||||||||||||||||||||
| 105 | 41 | ▶ | my($self, $years) = @_; | ||||||||||||||||||||||||||
|
Subroutine
years_ago: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 41 | return DateTime->now->subtract(years => $years); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub years_from_now { | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my($self, $years) = @_; | ||||||||||||||||||||||||||
|
Subroutine
years_from_now: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 0 | return DateTime->now->add(years => $years); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 7 | 1; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head1 NAME | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Valiant::Validator::Date - Verify that a value is is a standard Date (YYY-MM-DD) | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | package Local::Test::Date; | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | use Moo; | ||||||||||||||||||||||||||||
| 125 | use Valiant::Validations; | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | has birthday => (is=>'ro'); | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | validates birthday => ( | ||||||||||||||||||||||||||||
| 130 | date => { | ||||||||||||||||||||||||||||
| 131 | min => sub { pop->years_ago(120) }, # Oldest person I think... | ||||||||||||||||||||||||||||
| 132 | max => sub { pop->now }, | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | my $object = Local::Test::Date->new(birthday=>'2100-01-01'); | ||||||||||||||||||||||||||||
| 137 | $object->validate; | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | warn $object->errors->_dump; | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | $VAR1 = { | ||||||||||||||||||||||||||||
| 142 | 'birthday' => [ | ||||||||||||||||||||||||||||
| 143 | 'chosen date can't be above {{max}}', # In real life {{max}} would be | ||||||||||||||||||||||||||||
| 144 | # interpolated as DateTime->now | ||||||||||||||||||||||||||||
| 145 | ] | ||||||||||||||||||||||||||||
| 146 | }; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Validates a string pattern to make sure its in a standard date (YYYY-MM-DD) format, | ||||||||||||||||||||||||||||
| 151 | which is commonly used in databases as a Date field and its also the canonical | ||||||||||||||||||||||||||||
| 152 | pattern for the HTML5 input date type. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Can accept a 'min' and 'max' attribute, which should be either a string in the | ||||||||||||||||||||||||||||
| 155 | standard form or a M<DateTime> object. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | If you are using the Form helpers the max and min attributes can be reflected into | ||||||||||||||||||||||||||||
| 158 | the date input type automatically. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | This validator supports the following attributes: | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head2 pattern | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | This is a string pattern that is used by L<DateTime::Format::Strptime> that your | ||||||||||||||||||||||||||||
| 167 | date value must conform to (that is it must parse into a L<DateTime> object or the | ||||||||||||||||||||||||||||
| 168 | validation fails). The default is '%Y-%m-%d'. This is a common database format and | ||||||||||||||||||||||||||||
| 169 | is also used by HTML5 input date type fields. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head2 min | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | If provided set a bottom limit on the allowed date. Either a string in YYYY-MM-DD | ||||||||||||||||||||||||||||
| 174 | format or a L<DateTime> object. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Value may also be a coderef so that you can set dynamic dates (such as always today) | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head2 max | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | If provided set an upper limit on the allowed date. Either a string in YYYY-MM-DD | ||||||||||||||||||||||||||||
| 181 | format or a L<DateTime> object. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | Value may also be a coderef so that you can set dynamic dates (such as always today) | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head2 cb | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | A code reference that lets you create custom validation logic. This is basically the | ||||||||||||||||||||||||||||
| 188 | same as the 'With' validator expect its only called IF the value is in valid date | ||||||||||||||||||||||||||||
| 189 | format and you get that date inflated into a L<DateTime> object instead of the raw | ||||||||||||||||||||||||||||
| 190 | string value. This makes it a little less work for you since you can skip those extra | ||||||||||||||||||||||||||||
| 191 | checks. Also the coderef will receive the validator type instance as the third argument | ||||||||||||||||||||||||||||
| 192 | so that you can take advantage of the type helpers (see below L<\HELPERS>). | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | package MyRecord | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | use Moo; | ||||||||||||||||||||||||||||
| 197 | use Valiant::Validations; | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | has attribute => (is=>'ro'); | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | validates attribute => ( | ||||||||||||||||||||||||||||
| 202 | date => +{ | ||||||||||||||||||||||||||||
| 203 | min => sub { pop->years_ago(10) }, | ||||||||||||||||||||||||||||
| 204 | max => sub { pop->now }, | ||||||||||||||||||||||||||||
| 205 | cb => \&my_special_method, | ||||||||||||||||||||||||||||
| 206 | }, | ||||||||||||||||||||||||||||
| 207 | ); | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | sub my_special_method { | ||||||||||||||||||||||||||||
| 210 | my ($self, $dt, $type) = @_; | ||||||||||||||||||||||||||||
| 211 | # In this case $dt is a DateTime object inflated from the value | ||||||||||||||||||||||||||||
| 212 | # of 'attribute'. This method won't get called if we previously | ||||||||||||||||||||||||||||
| 213 | # determine that the value isn't in proper YYY-MM-DD format. | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | # Custom validation stuff... | ||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head2 below_min_msg | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =head2 above_max_msg | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head2 invalid_date_msg | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | The error message / tag associated with the given validation failures. Default messages | ||||||||||||||||||||||||||||
| 225 | are provided. | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =head1 HELPERS | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | This validator provides the following helpers. These basically just wrap L<DateTime> | ||||||||||||||||||||||||||||
| 230 | and L<DateTime::Format::Strptime> so you can avoid having to create your own in your | ||||||||||||||||||||||||||||
| 231 | record / object classes. | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =head2 datetime | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | Returns a raw blessed L<DateTime> object. If you pass a hash of arguments, those will | ||||||||||||||||||||||||||||
| 236 | be passed to C<new>. | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | =head2 now | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | returns L<DateTime> now | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =head2 years_ago | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =head2 years_from_now | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | Return a L<DateTime> object that is now plus or minus a given number of years. | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =head2 is_future | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =head2 is_past | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | Given a L<DateTime> object (such as the value you are trying to validate), return true | ||||||||||||||||||||||||||||
| 253 | or false if it is either in the future or in the past. | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =head1 SHORTCUT FORM | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | This validator supports the follow shortcut forms: | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | validates attribute => ( date => 1, ... ); | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | Which is the same as: | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | validates attribute => ( | ||||||||||||||||||||||||||||
| 264 | date => +{ }, | ||||||||||||||||||||||||||||
| 265 | ); | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | Not many saved characters but makes usage syntactically regular across validators. | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | You can also invoke a custom callback with a shortcut | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | validates attribute => ( date => \&my_special_method, ... ); | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | sub my_special_method { | ||||||||||||||||||||||||||||
| 274 | my ($self, $dt, $type) = @_; | ||||||||||||||||||||||||||||
| 275 | # Custom validation stuff | ||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | Which is the same as: | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | validates attribute => ( | ||||||||||||||||||||||||||||
| 281 | date => +{ | ||||||||||||||||||||||||||||
| 282 | cb => \&my_special_method, | ||||||||||||||||||||||||||||
| 283 | }, | ||||||||||||||||||||||||||||
| 284 | ); | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | Lastly you can specify that the date must be either future or past with a shortcut: | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | validates attribute => ( date => 'is_future', ... ); | ||||||||||||||||||||||||||||
| 289 | validates attribute => ( date => 'is_past', ... ); | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | Which is the same as: | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | validates attribute => ( | ||||||||||||||||||||||||||||
| 294 | date => +{ | ||||||||||||||||||||||||||||
| 295 | min => sub { pop->is_future }, | ||||||||||||||||||||||||||||
| 296 | max => sub { pop->is_past } | ||||||||||||||||||||||||||||
| 297 | }, | ||||||||||||||||||||||||||||
| 298 | ); | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =head1 GLOBAL PARAMETERS | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | This validator supports all the standard shared parameters: C<if>, C<unless>, | ||||||||||||||||||||||||||||
| 303 | C<message>, C<strict>, C<allow_undef>, C<allow_blank>. | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>. | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | See L<Valiant> | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | See L<Valiant> | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | =cut | ||||||||||||||||||||||||||||