Hi,
I had a node form which an interval field was required. If the field value was empty drupal would stop the form from submitting but would not display an error message.

Drupal relies on the #title element to display required form errors
from form.inc:


// Although discouraged, a #title is not mandatory for form elements. In
        // case there is no #title, we cannot set a form error message.
        // Instead of setting no #title, form constructors are encouraged to set
        // #title_display to 'invisible' to improve accessibility.
        if (isset($elements['#title'])) {
          form_error($elements, $t('!name field is required.', array('!name' => $elements['#title'])));
        }
        else {
          form_error($elements);
        }

Adding the title and title_display solved the issue for me

function interval_element_process($element, &$form_state, $form) {
  $value = !empty($element['#default_value'])?$element['#default_value']:array('interval' => NULL, 'period' => NULL);
  $element['interval'] = array(
    '#type' => 'textfield',
    '#default_value' => $value['interval'],
    '#required' => $element['#required'],
    '#title' => $element['#title'],
    '#title_display' => 'invisible',
  );

Patch coming.

Comments

iamjon’s picture

Status: Active » Needs review
StatusFileSize
new409 bytes

Patch and Status

iamjon’s picture

StatusFileSize
new450 bytes

Whoops. Patched the wrong section of code.

iamjon’s picture

Another Caveat is that your css should have something like

label.element-invisible {
display:none;
}

Or else the label shows up twice after ajax.

larowlan’s picture

Can we verify that $element['#title'] isn't susceptible to XSS attacks here (create a field with a title thus:

<script type="text/javascript">alert('yo');</script>
iamjon’s picture

Like wrapping it in check_plain or filter_xss? If yes I don't see why not and I'll adjust the patch.

monstrfolk’s picture

Fixing this issue created another issue with vague error messages on form submit.

This is fixed in https://www.drupal.org/node/2721765 and also has this patch integrated with it.