From 94b0db421bbc1ef256132c4c8c7675bc0aa5211c Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 2 Feb 2022 13:53:24 -0400 Subject: [PATCH 1/4] Hypothetical... --- .../FieldType/ExtendedDateTimeFormat.php | 3 +++ src/Plugin/Validation/Constraint/EDTF.php | 18 ++++++++++++++++ .../Validation/Constraint/EDTFValidator.php | 21 +++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/Plugin/Validation/Constraint/EDTF.php create mode 100644 src/Plugin/Validation/Constraint/EDTFValidator.php diff --git a/src/Plugin/Field/FieldType/ExtendedDateTimeFormat.php b/src/Plugin/Field/FieldType/ExtendedDateTimeFormat.php index 643021a..a17b194 100644 --- a/src/Plugin/Field/FieldType/ExtendedDateTimeFormat.php +++ b/src/Plugin/Field/FieldType/ExtendedDateTimeFormat.php @@ -14,6 +14,9 @@ * description = @Translation("Extended Date Time Format field"), * default_formatter = "edtf_default", * default_widget = "edtf_default", + * constraints = { + * "EDTF" = {}, + * }, * ) */ class ExtendedDateTimeFormat extends StringItem { diff --git a/src/Plugin/Validation/Constraint/EDTF.php b/src/Plugin/Validation/Constraint/EDTF.php new file mode 100644 index 0000000..72431e3 --- /dev/null +++ b/src/Plugin/Validation/Constraint/EDTF.php @@ -0,0 +1,18 @@ +getFieldDefinition(); + ddm($settings, 'qwer'); + dsm($settings, 'asdf'); + + // TODO: The validation things, using the field's configuration. + } + +} From a71e86bc8ec0ec405f31e1a62310619fca54f7cd Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 2 Feb 2022 15:15:11 -0400 Subject: [PATCH 2/4] Basic validation... ... to be sure that the field at least contains _some_ form of EDTF. --- src/Plugin/Validation/Constraint/EDTF.php | 2 +- .../Validation/Constraint/EDTFValidator.php | 27 +++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/Plugin/Validation/Constraint/EDTF.php b/src/Plugin/Validation/Constraint/EDTF.php index 72431e3..abb2463 100644 --- a/src/Plugin/Validation/Constraint/EDTF.php +++ b/src/Plugin/Validation/Constraint/EDTF.php @@ -13,6 +13,6 @@ */ class EDTF extends Constraint { - public $invalidStuff = '%value is not valid EDTF.'; + public $invalid = '%value is not valid EDTF: %verbose'; } diff --git a/src/Plugin/Validation/Constraint/EDTFValidator.php b/src/Plugin/Validation/Constraint/EDTFValidator.php index 27863a7..b4a88ff 100644 --- a/src/Plugin/Validation/Constraint/EDTFValidator.php +++ b/src/Plugin/Validation/Constraint/EDTFValidator.php @@ -2,20 +2,37 @@ namespace Drupal\controlled_access_terms\Plugin\Validation\Constraint; +use Drupal\controlled_access_terms\EDTFUtils; + use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; +use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** - * Validation. + * EDTF validation handler. */ class EDTFValidator extends ConstraintValidator { + /** + * {@inheritdoc} + */ public function validate($value, Constraint $constraint) { - $settings = $value->getFieldDefinition(); - ddm($settings, 'qwer'); - dsm($settings, 'asdf'); + if (!$constraint instanceof EDTF) { + throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\EDTF'); + } + if (NULL === $value) { + return; + } + + foreach ($value->getValue() as $val) { + foreach (EDTFUtils::validate($val, TRUE, TRUE, FALSE) as $error) { + $this->context->buildViolation($constraint->invalid) + ->setParameter('%value', $val) + ->setParameter('%verbose', $error) + ->addViolation(); + } + } - // TODO: The validation things, using the field's configuration. } } From 7a69822787e19f17b8a901b879bc8839bcde4503 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 2 Feb 2022 15:23:24 -0400 Subject: [PATCH 3/4] Somewhat more straight-forward reference to the desired class. --- src/Plugin/Validation/Constraint/EDTFValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/Validation/Constraint/EDTFValidator.php b/src/Plugin/Validation/Constraint/EDTFValidator.php index b4a88ff..e49e83c 100644 --- a/src/Plugin/Validation/Constraint/EDTFValidator.php +++ b/src/Plugin/Validation/Constraint/EDTFValidator.php @@ -18,7 +18,7 @@ class EDTFValidator extends ConstraintValidator { */ public function validate($value, Constraint $constraint) { if (!$constraint instanceof EDTF) { - throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\EDTF'); + throw new UnexpectedTypeException($constraint, EDTF::class); } if (NULL === $value) { return; From 569e38df7dc1bda942586c2bd43475c430d087cb Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Thu, 3 Feb 2022 17:19:49 -0400 Subject: [PATCH 4/4] Coding standards. --- src/Plugin/Validation/Constraint/EDTF.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Plugin/Validation/Constraint/EDTF.php b/src/Plugin/Validation/Constraint/EDTF.php index abb2463..7acfd78 100644 --- a/src/Plugin/Validation/Constraint/EDTF.php +++ b/src/Plugin/Validation/Constraint/EDTF.php @@ -5,6 +5,8 @@ use Symfony\Component\Validator\Constraint; /** + * EDTF constraint plugin. + * * @Constraint( * id = "EDTF", * label = @Translation("EDTF", context = "Validation"), @@ -13,6 +15,11 @@ */ class EDTF extends Constraint { + /** + * Invalid format message template. + * + * @var string + */ public $invalid = '%value is not valid EDTF: %verbose'; }