| @@ 377-448 (lines=72) @@ | ||
| 374 | * @param Validator $validator |
|
| 375 | * @return bool |
|
| 376 | */ |
|
| 377 | public function validate($validator) |
|
| 378 | { |
|
| 379 | // Don't validate empty fields |
|
| 380 | if (empty($this->rawValue)) { |
|
| 381 | return true; |
|
| 382 | } |
|
| 383 | ||
| 384 | // We submitted a value, but it couldn't be parsed |
|
| 385 | if (empty($this->value)) { |
|
| 386 | $validator->validationError( |
|
| 387 | $this->name, |
|
| 388 | _t( |
|
| 389 | 'SilverStripe\\Forms\\DateField.VALIDDATEFORMAT2', |
|
| 390 | "Please enter a valid date format ({format})", |
|
| 391 | ['format' => $this->getDateFormat()] |
|
| 392 | ) |
|
| 393 | ); |
|
| 394 | return false; |
|
| 395 | } |
|
| 396 | ||
| 397 | // Check min date |
|
| 398 | $min = $this->getMinDate(); |
|
| 399 | if ($min) { |
|
| 400 | $oops = strtotime($this->value) < strtotime($min); |
|
| 401 | if ($oops) { |
|
| 402 | $validator->validationError( |
|
| 403 | $this->name, |
|
| 404 | _t( |
|
| 405 | 'SilverStripe\\Forms\\DateField.VALIDDATEMINDATE', |
|
| 406 | "Your date has to be newer or matching the minimum allowed date ({date})", |
|
| 407 | [ |
|
| 408 | 'date' => sprintf( |
|
| 409 | '<time datetime="%s">%s</time>', |
|
| 410 | $min, |
|
| 411 | $this->internalToFrontend($min) |
|
| 412 | ) |
|
| 413 | ] |
|
| 414 | ), |
|
| 415 | ValidationResult::TYPE_ERROR, |
|
| 416 | ValidationResult::CAST_HTML |
|
| 417 | ); |
|
| 418 | return false; |
|
| 419 | } |
|
| 420 | } |
|
| 421 | ||
| 422 | // Check max date |
|
| 423 | $max = $this->getMaxDate(); |
|
| 424 | if ($max) { |
|
| 425 | $oops = strtotime($this->value) > strtotime($max); |
|
| 426 | if ($oops) { |
|
| 427 | $validator->validationError( |
|
| 428 | $this->name, |
|
| 429 | _t( |
|
| 430 | 'SilverStripe\\Forms\\DateField.VALIDDATEMAXDATE', |
|
| 431 | "Your date has to be older or matching the maximum allowed date ({date})", |
|
| 432 | [ |
|
| 433 | 'date' => sprintf( |
|
| 434 | '<time datetime="%s">%s</time>', |
|
| 435 | $max, |
|
| 436 | $this->internalToFrontend($max) |
|
| 437 | ) |
|
| 438 | ] |
|
| 439 | ), |
|
| 440 | ValidationResult::TYPE_ERROR, |
|
| 441 | ValidationResult::CAST_HTML |
|
| 442 | ); |
|
| 443 | return false; |
|
| 444 | } |
|
| 445 | } |
|
| 446 | ||
| 447 | return true; |
|
| 448 | } |
|
| 449 | ||
| 450 | /** |
|
| 451 | * Get locale to use for this field |
|
| @@ 560-631 (lines=72) @@ | ||
| 557 | * @param Validator $validator |
|
| 558 | * @return bool |
|
| 559 | */ |
|
| 560 | public function validate($validator) |
|
| 561 | { |
|
| 562 | // Don't validate empty fields |
|
| 563 | if (empty($this->rawValue)) { |
|
| 564 | return true; |
|
| 565 | } |
|
| 566 | ||
| 567 | // We submitted a value, but it couldn't be parsed |
|
| 568 | if (empty($this->value)) { |
|
| 569 | $validator->validationError( |
|
| 570 | $this->name, |
|
| 571 | _t( |
|
| 572 | 'DatetimeField.VALIDDATETIMEFORMAT', |
|
| 573 | "Please enter a valid date and time format ({format})", |
|
| 574 | ['format' => $this->getDatetimeFormat()] |
|
| 575 | ) |
|
| 576 | ); |
|
| 577 | return false; |
|
| 578 | } |
|
| 579 | ||
| 580 | // Check min date (in server timezone) |
|
| 581 | $min = $this->getMinDatetime(); |
|
| 582 | if ($min) { |
|
| 583 | $oops = strtotime($this->value) < strtotime($min); |
|
| 584 | if ($oops) { |
|
| 585 | $validator->validationError( |
|
| 586 | $this->name, |
|
| 587 | _t( |
|
| 588 | 'DatetimeField.VALIDDATETIMEMINDATE', |
|
| 589 | "Your date has to be newer or matching the minimum allowed date and time ({datetime})", |
|
| 590 | [ |
|
| 591 | 'datetime' => sprintf( |
|
| 592 | '<time datetime="%s">%s</time>', |
|
| 593 | $min, |
|
| 594 | $this->internalToFrontend($min) |
|
| 595 | ) |
|
| 596 | ] |
|
| 597 | ), |
|
| 598 | ValidationResult::TYPE_ERROR, |
|
| 599 | ValidationResult::CAST_HTML |
|
| 600 | ); |
|
| 601 | return false; |
|
| 602 | } |
|
| 603 | } |
|
| 604 | ||
| 605 | // Check max date (in server timezone) |
|
| 606 | $max = $this->getMaxDatetime(); |
|
| 607 | if ($max) { |
|
| 608 | $oops = strtotime($this->value) > strtotime($max); |
|
| 609 | if ($oops) { |
|
| 610 | $validator->validationError( |
|
| 611 | $this->name, |
|
| 612 | _t( |
|
| 613 | 'DatetimeField.VALIDDATEMAXDATETIME', |
|
| 614 | "Your date has to be older or matching the maximum allowed date and time ({datetime})", |
|
| 615 | [ |
|
| 616 | 'datetime' => sprintf( |
|
| 617 | '<time datetime="%s">%s</time>', |
|
| 618 | $max, |
|
| 619 | $this->internalToFrontend($max) |
|
| 620 | ) |
|
| 621 | ] |
|
| 622 | ), |
|
| 623 | ValidationResult::TYPE_ERROR, |
|
| 624 | ValidationResult::CAST_HTML |
|
| 625 | ); |
|
| 626 | return false; |
|
| 627 | } |
|
| 628 | } |
|
| 629 | ||
| 630 | return true; |
|
| 631 | } |
|
| 632 | ||
| 633 | public function performReadonlyTransformation() |
|
| 634 | { |
|