Problem/Motivation

When I have a validation error on some form elements, the following warning message is thrown:

Warning: Undefined array key "id" in uswds_base_preprocess_form_element() (line 17 of /app/docroot/themes/contrib/uswds_base/preprocess/element/form_element.preprocess.inc).

It doesn't seem to happen with text inputs, but does on textarea elements. I suspect it is also happening on other element types.

Looking at <uswds_base_preprocess_form_element it seems not all form elements have their id set at: $variables['element']['#attributes']['id']. It does seem that the id is always available at: $variables['element']['#id'] however.

Steps to reproduce

  1. Build or use a form with a textarea element.
  2. Trigger a server-side error on the textarea element, such as being required.
  3. A warning Undefined array key "id" in uswds_base_preprocess_form_element() should be printed in the logs.

Proposed resolution

In uswds_base_preprocess_form_element conditionally check that the $variables['element']['#attributes']['id'] or $variables['element']['#id'] items exist before adding them to $variables['error_id'].

Remaining tasks

  1. Confirm bug and agree on solution.
  2. Create patch implementing solution.

User interface changes

N/A

API changes

N/A

Data model changes

N/A

CommentFileSizeAuthor
#7 Screen Shot 2022-08-29 at 3.47.04 PM.png42.03 KBpcate

Issue fork uswds_base-3302483

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

PCate created an issue. See original summary.

pcate’s picture

Status: Active » Needs review
pcate’s picture

Issue summary: View changes
smustgrave’s picture

I tried testing this

Added a text(plain, long) field to a content type
Made the field required
Enabled uswds as the theme to use when creating content
Create a piece of content and NOT fill in the textarea. Click save
Didn't see any errors in logs

mferanda’s picture

Status: Needs review » Postponed (maintainer needs more info)
pcate’s picture

StatusFileSize
new42.03 KB

I tried testing this

Added a text(plain, long) field to a content type
Made the field required
Enabled uswds as the theme to use when creating content
Create a piece of content and NOT fill in the textarea. Click save
Didn't see any errors in logs

Did you see a Drupal error message on the page about the field being required after you saved? Also, just to confirm you are logging warnings not just errors?

Attached is a screenshot of the warning I'm seeing.

  • mferanda committed 32aa2d2 on 3.0.x authored by PCate
    Issue #3302483 by PCate: Undefined array key "id" in...
mferanda’s picture

Looks like a solid enough change... I'm good to include for RC2

mferanda’s picture

Status: Postponed (maintainer needs more info) » Needs review
mferanda’s picture

Status: Needs review » Fixed
mferanda’s picture

Status: Fixed » Closed (fixed)