Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FAU-411] Add campo-key field / Add form fields validation rules #130

Merged
merged 9 commits into from
May 24, 2024

Conversation

amiut
Copy link
Collaborator

@amiut amiut commented May 10, 2024

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes/features)
  • Docs have been added/updated (for bug fixes/features)

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Feature

What is the current behavior? (You can also link to an open issue here)
https://inpsyde.atlassian.net/browse/FAU-411

What is the new behavior (if this is a feature change)?

  • Added campo-key field for the following taxonomies: abschluss, area_of_study, faechergruppe, standort with validation rules.
  • Add validation error messages both on backend and javascript (HTML5 validation).

Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)
No

Other information:

Copy link
Collaborator

@o-samaras o-samaras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on that. Left a couple of suggestions and a question.

Copy link
Contributor

@shvlv shvlv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach is much more straightforward and looks good to me. The single suggestion from my side is if we add the validationPattern to most generic interface all implementations should provide actual functionality. I can imagine something like:

Subject: [PATCH] refactor: move functionality to FormFieldValidationRule and FormFieldValidationRuleSet
---
Index: src/Infrastructure/Dashboard/TermMeta/TermMetaFieldsValidator.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/Infrastructure/Dashboard/TermMeta/TermMetaFieldsValidator.php b/src/Infrastructure/Dashboard/TermMeta/TermMetaFieldsValidator.php
--- a/src/Infrastructure/Dashboard/TermMeta/TermMetaFieldsValidator.php	(revision 1da837a286fc458fd85c3ae1170d2d4c53225d5d)
+++ b/src/Infrastructure/Dashboard/TermMeta/TermMetaFieldsValidator.php	(date 1715590469638)
@@ -15,8 +15,8 @@
         }
 
         foreach ($termMetaFields as $termMetaField) {
-            // Only campo key field supports validation.
-            if (! $termMetaField instanceof CampoKeyTermMetaField) {
+            $validationPattern = $termMetaField->validationPattern();
+            if (is_null($validationPattern)) {
                 continue;
             }
 
@@ -32,11 +32,6 @@
             if ($sanitizedValue === '') {
                 continue;
             }
-
-            $validationPattern = $termMetaField->validationPattern();
-            if (is_null($validationPattern)) {
-                continue;
-            }
 
             if (! $validationPattern->matches($sanitizedValue)) {
                 return new WP_Error(
Index: src/Infrastructure/Dashboard/TermMeta/InputTermMetaField.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/Infrastructure/Dashboard/TermMeta/InputTermMetaField.php b/src/Infrastructure/Dashboard/TermMeta/InputTermMetaField.php
--- a/src/Infrastructure/Dashboard/TermMeta/InputTermMetaField.php	(revision 1da837a286fc458fd85c3ae1170d2d4c53225d5d)
+++ b/src/Infrastructure/Dashboard/TermMeta/InputTermMetaField.php	(date 1715590287875)
@@ -7,10 +7,11 @@
 class InputTermMetaField implements TermMetaField
 {
     public function __construct(
-        protected string $key,
-        protected string $title,
-        protected string $description = '',
-        protected string $type = 'text',
+        private string $key,
+        private string $title,
+        private string $description = '',
+        private string $type = 'text',
+        private ?TermMetaFieldValidationPattern $validationPattern = null,
     ) {
     }
 
@@ -36,6 +37,17 @@
 
     public function templateData(mixed $value): array
     {
+        $showInRest = true;
+
+        if (! is_null($this->validationPattern)) {
+            $showInRest = [
+                'schema' => [
+                    'type' => 'string',
+                    'pattern' => $this->validationPattern->pattern(),
+                ],
+            ];
+        }
+
         return [
             'key' => $this->key,
             'value' => (string) $value,
@@ -43,6 +55,7 @@
             'description' => $this->description,
             'type' => $this->type,
             'validationPattern' => $this->validationPattern(),
+            'show_in_rest' => $showInRest,
         ];
     }
 
@@ -60,6 +73,6 @@
 
     public function validationPattern(): ?TermMetaFieldValidationPattern
     {
-        return null;
+        return $this->validationPattern;
     }
 }
Index: src/Infrastructure/Dashboard/TermMeta/CampoKeyTermMetaField.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/Infrastructure/Dashboard/TermMeta/CampoKeyTermMetaField.php b/src/Infrastructure/Dashboard/TermMeta/CampoKeyTermMetaField.php
--- a/src/Infrastructure/Dashboard/TermMeta/CampoKeyTermMetaField.php	(revision 1da837a286fc458fd85c3ae1170d2d4c53225d5d)
+++ b/src/Infrastructure/Dashboard/TermMeta/CampoKeyTermMetaField.php	(date 1715590469642)
@@ -9,67 +9,15 @@
     public const KEY = 'uniquename';
 
     public function __construct(
-        protected string $description,
-        private ?TermMetaFieldValidationPattern $validationPattern = null,
+        string $description,
+        TermMetaFieldValidationPattern $validationPattern = null,
     ) {
 
         parent::__construct(
             self::KEY,
             __('Campo Key', 'fau-degree-program'),
             $description,
-            'text'
+            validationPattern: $validationPattern,
         );
     }
-
-    public function title(): string
-    {
-        return $this->title;
-    }
-
-    public function sanitize(mixed $value): string
-    {
-        return sanitize_text_field((string) $value);
-    }
-
-    public function templateName(): string
-    {
-        return 'input-term';
-    }
-
-    public function validationPattern(): ?TermMetaFieldValidationPattern
-    {
-        return $this->validationPattern;
-    }
-
-    public function templateData(mixed $value): array
-    {
-        return [
-            'key' => $this->key(),
-            'value' => (string) $value,
-            'title' => $this->title,
-            'description' => $this->description,
-            'type' => $this->type,
-            'validationPattern' => $this->validationPattern,
-        ];
-    }
-
-    public function metaArgs(): array
-    {
-        $schema = ['type' => 'string'];
-
-        if (! is_null($this->validationPattern)) {
-            $schema['pattern'] = $this->validationPattern->pattern();
-        }
-
-        return [
-            'type' => 'string',
-            'description' => $this->description,
-            'single' => true,
-            'default' => '',
-            'sanitize_callback' => [$this, 'sanitize'],
-            'show_in_rest' => [
-                'schema' => $schema,
-            ],
-        ];
-    }
 }

Alternatively, you can consider a validation pattern as a specific implementation. But this way the getter should be removed from the interface (maybe not the best solution).

Copy link
Contributor

@shvlv shvlv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Collaborator

@zhyian zhyian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for taking care of this.

Copy link
Collaborator

@tyrann0us tyrann0us left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this! I left a couple of wording-related suggestions to ensure the strings are as aligned as possible and easy to understand for the users.

Co-authored-by: Philipp Bammes <8144115+tyrann0us@users.noreply.github.com>
Co-authored-by: Philipp Bammes <8144115+tyrann0us@users.noreply.github.com>
Copy link
Collaborator

@tyrann0us tyrann0us left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! I probably wouldn't have touched the state (disabled/enabled) of the form submit button but let the browser handle it natively. However, this is much better and feels more native than the first approach. So, let's keep this for now and await client testing.
Thanks for this great work! 💪🏽

@amiut
Copy link
Collaborator Author

amiut commented May 22, 2024

Thanks a lot! I probably wouldn't have touched the state (disabled/enabled) of the form submit button but let the browser handle it natively. However, this is much better and feels more native than the first approach. So, let's keep this for now and await client testing. Thanks for this great work! 💪🏽

Disabling the submit button is to prevent the form from being submitted in add term form which is an ajax-form and is controlled by a javascript file from WordPress core, I think we need it to prevent the other event listener sending form data via Ajax when the form is invalid.

Copy link
Collaborator

@o-samaras o-samaras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. LGTM!

@amiut amiut merged commit 77a8f78 into dev May 24, 2024
9 checks passed
@amiut amiut deleted the FAU-411-alternative branch May 24, 2024 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants