Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

[SDPAP-7155] webform validation #77

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

vincent-gao
Copy link
Contributor

@vincent-gao vincent-gao commented Apr 3, 2023

Jira

https://digital-vic.atlassian.net/browse/SDPAP-7155

Motivation

default webform JSONAPI endpoints don't support any validations, and it also doesn't support HTTP Unprocessable Content response with details of why the data validation failed.

Drupal JSONAPI also limits us from modifying the response structure or adding custom validations.

so we need a custom endpoint to handle data validation and return a reasonable response.

Change

  1. defined a custom endpoint
    • api/v1/webform/tide_webform_content_rating/add
  2. headers:
    • Content-Type: application/vnd.api+json
  3. body example
        {
          "data": {
            "type": "webform_submission--tide_webform_content_rating",
            "attributes": {
              "remote_addr": "1.2.3.4",
              "data": "url: '/home'\nwas_this_page_helpful: 'Yes'\nemail_addr: 'asdfacom'\nphone_number: '045123'"
            }
          }
        }
    
  4. support list
    • It supports verifying:
      • email
      • phone number
    • it supports verifying empty or unset fields that are required.
    • It supports Pattern

All form validations below will be reflected in JSONAPI webform endpoints, including Required message and Pattern message, but we don't support unique as we don't keep submissions in most cases.
CleanShot 2023-06-05 at 15 13 05

Response

{
    "jsonapi": {
        "version": "1.0",
        "meta": {
            "links": {
                "self": {
                    "href": "http://jsonapi.org/format/1.0/"
                }
            }
        }
    },
    "errors": [
        {
            "title": "Unprocessable Entity",
            "status": "422",
            "detail": "email_addr|Field email_addr is required.",
            "links": {
                "via": {
                    "href": "http://content-sdp.docker.internal:8888/api/v1/webform/tide_webform_content_rating/add?XDEBUG_SESSION=PHPSTORM"
                }
            }
        },
        {
            "title": "Unprocessable Entity",
            "status": "422",
            "detail": "phone_number|Please provide a valid Phone number.",
            "links": {
                "via": {
                    "href": "http://content-sdp.docker.internal:8888/api/v1/webform/tide_webform_content_rating/add?XDEBUG_SESSION=PHPSTORM"
                }
            }
        }
    ]
}

@vincent-gao vincent-gao force-pushed the feature/SDPAP-7155-webform-validation branch from 3a21e60 to 4d2d699 Compare April 10, 2023 23:17
@vincent-gao vincent-gao force-pushed the feature/SDPAP-7155-webform-validation branch 5 times, most recently from c20eb1b to 715ec9d Compare April 14, 2023 07:03
1. Adds a kernel test
2. Remove outdated verification steps from tide_webform.module
@vincent-gao vincent-gao force-pushed the feature/SDPAP-7155-webform-validation branch from 715ec9d to 2df2e6b Compare April 17, 2023 04:15
@vincent-gao vincent-gao force-pushed the feature/SDPAP-7155-webform-validation branch 2 times, most recently from 82fc80a to ab21cb0 Compare June 5, 2023 04:25
@vincent-gao vincent-gao force-pushed the feature/SDPAP-7155-webform-validation branch from ab21cb0 to 10e0439 Compare June 5, 2023 04:57
@anthony-malkoun
Copy link
Contributor

@vincent-gao for my understanding, this means validation is only supported for the was this page helpful webform? There's not a more generic way to get it into all forms or we have to write code each time?

@vincent-gao
Copy link
Contributor Author

for my understanding, this means validation is only supported for the was this page helpful webform? There's not a more generic way to get it into all forms or we have to write code each time?

hey @anthony-malkoun , it supports all webforms. please take a look into https://github.com/dpc-sdp/tide_webform/pull/77/files#diff-c3e4a54bf03798e28f32ae99b7aa80077275b1e6e1a45cb049ebef34d3918513R2
, users can change {webform} with a webform_id

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants