class DateTimeFormatConstraintValidator

Same name in other branches
  1. 9 core/modules/datetime/src/Plugin/Validation/Constraint/DateTimeFormatConstraintValidator.php \Drupal\datetime\Plugin\Validation\Constraint\DateTimeFormatConstraintValidator
  2. 8.9.x core/modules/datetime/src/Plugin/Validation/Constraint/DateTimeFormatConstraintValidator.php \Drupal\datetime\Plugin\Validation\Constraint\DateTimeFormatConstraintValidator
  3. 10 core/modules/datetime/src/Plugin/Validation/Constraint/DateTimeFormatConstraintValidator.php \Drupal\datetime\Plugin\Validation\Constraint\DateTimeFormatConstraintValidator

Constraint validator for DateTime items to ensure the format is correct.

Hierarchy

Expanded class hierarchy of DateTimeFormatConstraintValidator

File

core/modules/datetime/src/Plugin/Validation/Constraint/DateTimeFormatConstraintValidator.php, line 14

Namespace

Drupal\datetime\Plugin\Validation\Constraint
View source
class DateTimeFormatConstraintValidator extends ConstraintValidator {
    
    /**
     * {@inheritdoc}
     */
    public function validate($item, Constraint $constraint) : void {
        
        /** @var \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem $item */
        if (isset($item)) {
            $value = $item->getValue()['value'];
            if (!is_string($value)) {
                $this->context
                    ->addViolation($constraint->badType);
            }
            else {
                $datetime_type = $item->getFieldDefinition()
                    ->getSetting('datetime_type');
                $format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
                $date = NULL;
                try {
                    $date = DateTimePlus::createFromFormat($format, $value, new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
                } catch (\InvalidArgumentException) {
                    $this->context
                        ->addViolation($constraint->badFormat, [
                        '@value' => $value,
                        '@format' => $format,
                    ]);
                    return;
                } catch (\UnexpectedValueException) {
                    $this->context
                        ->addViolation($constraint->badValue, [
                        '@value' => $value,
                        '@format' => $format,
                    ]);
                    return;
                }
                if ($date === NULL || $date->hasErrors()) {
                    $this->context
                        ->addViolation($constraint->badFormat, [
                        '@value' => $value,
                        '@format' => $format,
                    ]);
                }
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
DateTimeFormatConstraintValidator::validate public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.