Problem:
Empty title/label on validating multiple value text area field.

Screenshots:

Article
Clean Drupal 7.24 installation with an Article content type and Questions (multiple values) field as Long text with Text area widget

Error
You can see here the title is missing in validation error for Questions field.

Bug:
As you can see in the second image, the title is missing if you have multiple values. I debugged the _form_validate function where I see that it's trying to get the #title from each item of a field in $elements array. But the title is empty for those items.

Drupal 8:
Drupal 8 is using different UX for validation so this bug is not an issue for Drupal 8.

Comments

alesr’s picture

Issue summary: View changes
StatusFileSize
new17.92 KB
new10.04 KB
alesr’s picture

Status: Active » Needs review
StatusFileSize
new692 bytes

This is the code from _form_validate function in form.inc file where the problem is.

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

Those items in multiple values long text field all have $elements['#title'] set, but it is empty. Actually one (cheap) solution is to check if !empty() together with isset().

A patch is attached to nudge the bot.

ianthomas_uk’s picture

Status: Needs review » Needs work

This bug happens because we validate each of the multi-value items separately, so while the field itself has a title, each of the child elements doesn't. The patch on #2 hides the error message when this happens, rather than showing the incorrect error message.

This isn't really a great solution, as we still want to tell the user which field they need to complete. There's another error message that needs the field title higher up the function (line 1307) that does work. I suggest we get the title for missing required fields in the same way.

alesr’s picture

Status: Needs work » Needs review
StatusFileSize
new740 bytes

Here is the patch that is showing the field name as it is solved above in the code.
Still not the title/label that we would like to have, but much better than no title at all.

ianthomas_uk’s picture

Status: Needs review » Needs work

That's better, but I think while we're at it we should include $elements['#parents'][0] in the if statement, as we can now display a useful message if that is set.

Having said that, is $elements['#parents'][0] ever not set? Everywhere else in the function assumes that either $elements['#title'] or $elements['#parents'][0] will be set, so should we just do the same here? (i.e. remove the if/else and just have the new contents of the if).

oleg.medvedev’s picture

StatusFileSize
new1.04 KB
oleg.medvedev’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 6: empty-title-on-validation-2145483-6.patch, failed testing.

oleg.medvedev’s picture

Status: Needs work » Needs review
StatusFileSize
new1.21 KB
Rostislav Sergeenkov’s picture

Alternative idea is to add title to the 1-st item of multiple value field (since only the 1-st element from required field is validated and must not be empty) and hide it using $element['#title_display'] = 'invisible'. As a result field name appears in validation message. See patch and attached before_patching.png and after_patching.png

Status: Needs review » Needs work

The last submitted patch, 10: drupal_core-empty-title-on-validation-2145483-10.patch, failed testing.

Rostislav Sergeenkov’s picture

StatusFileSize
new1.16 KB
dcam’s picture

Status: Needs work » Needs review

Starting Testbot.

pkht’s picture

Status: Needs review » Reviewed & tested by the community

I had this exact issue in my current project.
Patch #12 solves it.
I've also tested it in a fresh install - success.

David_Rothstein’s picture

Version: 7.x-dev » 8.0.x-dev
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs backport to D7

This issue is reproducible in Drupal 8.

You do need to fill in the title field (to avoid the HTML5 client-side validation), which is different than the screenshot shown in the issue summary, but otherwise the steps to reproduce are essentially the same as for Drupal 7.

(And of course, Drupal 8 has server-side validation even on fields for which the client-side validation is enabled by default... so even if the HTML5 validation intercepted the error for this field too, it would still need to be fixed. But I'm able to reproduce it just fine without even turning that off.)

brunodbo’s picture

Version: 8.0.x-dev » 7.x-dev
Issue tags: -Needs backport to D7

Looks like this is fixed in D8 now. On a fresh D8 install, I disabled the HTML5 validation for the article form (by adding novalidate="novalidate" to the form element), and the validation message for a multivalue text field now includes the field name (and the value as well) - see attached screenshot.

The patch in #12 fixes the issue for me in D7.

brunodbo’s picture

StatusFileSize
new30.6 KB

And here is the aforementioned screenshot.

brunodbo’s picture

Status: Needs work » Needs review

Triggering testbot.

David_Rothstein’s picture

Status: Needs review » Closed (duplicate)
Related issues: +#980144: Issues with "required, multiple" fields in forms

Looks like #980144: Issues with "required, multiple" fields in forms fixed this for Drupal 8, and a backport to Drupal 7 is in progress there. That issue is older and the patch there looks farther along, so I'm closing this as a duplicate.