Required Form elements without a title doesn't show error

Created on 17 June 2011, over 13 years ago
Updated 18 October 2023, about 1 year ago

Problem/Motivation

If you have any kind of form element without a title and it's required you will not see that it's actually required on form validation.

Proposed resolution

The problem is, that form_get_error() returns an empty string which is implicitly casted to boolean in the following statement:
if (isset($element['#parents']) && form_get_error($element))
In _form_set_class(), check for !== NULL.

Remaining tasks

This is no longer an issue in D8.

<!-- See https://drupal.org/core-mentoring/novice-tasks for tips on identifying novice tasks. Delete or add "Novice" from the Novice? column in the table below as appropriate. Uncomment tasks as the issue advances. Update the Complete? column to indicate when they are done, and maybe reference the comment number where they were done. -->

User interface changes

n/a

API changes

n/a

Original report by @dawehner

If you have any kind of form element without a title and it's required for example you will not see that it's actually required.

The problem is that #required uses

        if (isset($elements['#title'])) {
          form_error($elements, $t('!name field is required.', array('!name' => $elements['#title'])));
        }
        else {
          form_error($elements);
        }

So form_error set's a error = '';

Now in _form_set_class

  if (isset($element['#parents']) && form_get_error($element)) {
    $element['#attributes']['class'][] = 'error';
  }

But '' is a boolean false so there is no error message.

๐Ÿ› Bug report
Status

Needs work

Version

9.5

Component
Formย  โ†’

Last updated 5 days ago

Created by

๐Ÿ‡ฉ๐Ÿ‡ชGermany dawehner

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

  • Needs backport to D7

    After being applied to the 8.x branch, it should be considered for backport to the 7.x branch. Note: This tag should generally remain even after the backport has been written, approved, and committed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024