chicommons / maps

MIT License
6 stars 18 forks source link

Frontend: Form field error messaging #254

Open domdelorenzo opened 3 months ago

domdelorenzo commented 3 months ago

Objective

Display error messages on required fields when the fields are blank or invalid

Related components

Prior behavior:

If a form was submitted without all required fields,

  1. the json error response would be set in state on DirectoryAddUpdate.jsx and
  2. each Input component would parse the error json, find error keys that matched the input name, and display the associated message in red below the input. For example:
    • if the entityTypes input was empty, the the error object would be passed to each input
    • the Input with the property name=entityTypes would match the key entityTypes from the error object to it's own name property (const errorsArr = _.get(props.errors, props.name);
    • the errorArr would have a truthy value, and reveal a FormControl div containing the error message from the object
      {errorsArr && (
      <FormControl.Feedback type="invalid">
        {errorsArr.map((error, index) => (
          <div
            key={`field-error-${props.name}-${index}`}
            className="fieldError"
          >
            {error}
          </div>
        ))}
      </FormControl.Feedback>
      )}

Current behavior:

{
  "coop": {
    "contact_methods": [
      {
        "phone": [
          "The phone number entered is not valid."
        ]
      }
    ],
    "people": [
      {
        "first_name": [
          "This field may not be blank."
        ],
        "last_name": [
          "This field may not be blank."
        ],
        "contact_methods": [
          {
            "non_field_errors": [
              "Either an email or a phone number must be provided."
            ]
          }
        ]
      }
    ],
    "addresses": [
      {
        "address": {
          "county": [
            "This field may not be blank."
          ]
        }
      }
    ],
    "description": [
      "This field may not be blank."
    ],
    "tags": [
      "This field may not be blank."
    ]
  }
}

Suggestions