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.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | avoid-calling-empty-property-2342883-3.patch | 450 bytes | iamjon |
Comments
Comment #1
iamjon commentedPatch and Status
Comment #2
iamjon commentedWhoops. Patched the wrong section of code.
Comment #3
iamjon commentedAnother Caveat is that your css should have something like
label.element-invisible {
display:none;
}
Or else the label shows up twice after ajax.
Comment #4
larowlanCan we verify that $element['#title'] isn't susceptible to XSS attacks here (create a field with a title thus:
Comment #5
iamjon commentedLike wrapping it in check_plain or filter_xss? If yes I don't see why not and I'll adjust the patch.
Comment #6
monstrfolk commentedFixing 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.