This is a backport issue for:

#177124: Form validation messages appear in incorrect order

The following description was copied from the original issue.

Steps to reproduce:

1) Change the #weights of elements on a built-in Drupal form to reorder form elements. The registration form is a good choice.
2) Submit the form with errors in every validated field.
3) Notice that the order the validation messages appear in does not match the order of the fields on the form.

I've prepared a patch that solves this issue by sorting child elements before doing any validating in _form_validate(). Patch is built against Drupal 5.1.

Comments

hgoto created an issue. See original summary.

amit0212’s picture

This does not completely solve the issue. The errors are displayed in the order form_set_error() is called. This is controlled by the hook invocation order and what order the validate hooks look at the fields. This patch does of course solve the issue for empty required fields and any modules which walk through the fields in form order.

Patch

hgoto’s picture

@amit0212 thank you for your review. I guess you have reviewed the patch #4 in the original issue. But the newest patch for D7 is #52 and it's better to start with it.

hgoto’s picture

Here is a patch. I updated the patch #52 in the original issue with a test like one for D8.

The method name of the test case is testValidationErrorMessagesSortedWithWeight() which is same as I suggested comment #72 in the original issue.

I'd like someone to review the patch.

The last submitted patch, 4: sort_form_validation_message_with_weight-2850273-3.patch, failed testing.

Status: Needs review » Needs work
hgoto’s picture

In the patch #4, I used array bracket syntax which is not allowed in PHP 5.3 by mistake...

I updated the patch and hope this would pass the tests without CI error.

hgoto’s picture

StatusFileSize
new418 bytes

Interdiff.

Status: Needs review » Needs work
hgoto’s picture

Status: Needs work » Needs review

The test failed and passed as expected!

quotesbro’s picture

@hgoto thanks for the patch! It works for me.

I changed field titles to make the test more readable.

quotesbro’s picture

Issue tags: -Needs tests

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.