Problem/Motivation

When form validation errors occur in Views exposed forms when the View has AJAX enabled, the error messages are not displayed.

Steps to reproduce

Create a view with an exposed filter on the author
Enable ajax on the view
Use the view and submit the form with a non-existent username
No error message is shown.

Proposed resolution

Prepend status messages in the views ajax controller.

Original report

The applied validation error messages are not shown for a view exposed filter on failure when "Ajax" is on.

ajax-option

the messages are displayed again on next page load.

There is an older issue opened for Drupal 7

https://www.drupal.org/node/1833322

Recommended solution:


function MODULE_views_ajax_data_alter(&$commands, $view) {
$commands[] = ajax_command_prepend($commands[0]['selector'], theme('status_messages'));
}

Is the correct way to solve the problem or solution should be implemented in views module itself ?

CommentFileSizeAuthor
#70 interdiff-2977785-68-70.txt856 bytesacbramley
#70 2977785-70.patch5.9 KBacbramley
#68 interdiff-2977785-57-68.txt959 bytesacbramley
#68 2977785-68.patch5.33 KBacbramley
#62 interdiff_60_62.txt815 bytesameymudras
#62 2977785-62.patch5.79 KBameymudras
#60 interdiff_57_60.txt892 bytesameymudras
#60 2977785-60.patch5.78 KBameymudras
#57 interdiff-2977785-50-57.txt537 bytesacbramley
#57 2977785-57.patch5.63 KBacbramley
#56 interdiff_50-56.txt723 bytesprem suthar
#56 2977785-56.patch4.94 KBprem suthar
#54 interdiff_53-54.txt703 bytestanuj.
#54 2977785-54.patch1.23 KBtanuj.
#53 interdiff_50-53.txt4.27 KBsourabhjain
#53 2977785-53.patch1.23 KBsourabhjain
#50 interdiff-2977785-49-50.txt695 bytesacbramley
#50 2977785-50.patch4.93 KBacbramley
#49 interdiff-2977785-40-49.txt1.49 KBacbramley
#49 2977785-49.patch4.93 KBacbramley
#40 interdiff_35-40.txt511 bytesravi.shankar
#40 2977785-40.patch4.46 KBravi.shankar
#38 after-patch.png50.78 KBsnehalgaikwad
#38 before-patch.png32.58 KBsnehalgaikwad
#35 interdiff-32-35.txt1.42 KBhardik_patel_12
#35 2977785-35.patch4.68 KBhardik_patel_12
#32 interdiff-2977785-30-32.txt788 bytesmrinalini9
#32 2977785-32.patch4.45 KBmrinalini9
#30 interdiff-2977785-26-30.txt728 bytesmrinalini9
#30 2977785-30.patch4.44 KBmrinalini9
#26 interdiff_21-26.txt1.21 KBdeepak goyal
#26 2977785-26.patch4.88 KBdeepak goyal
#23 2977785-23-8.9.patch4.49 KBacbramley
#21 interdiff-2977785-15-21.txt1.99 KBacbramley
#21 2977785-21.patch4.5 KBacbramley
#15 2977785-15-test-only.patch2.4 KBacbramley
#15 2977785-15.patch6.19 KBacbramley
#12 2977785-12.patch3.78 KBkishor_kolekar
#10 2977785-10.patch2.1 KBhardik_patel_12
#5 views-ajax-messages-2977785-5.patch2.1 KBberdir
#5 head_ife_disabled.png22.22 KBberdir
#5 patch_ife.png25.3 KBberdir
#5 head_ife_refresh.png23.59 KBberdir
#5 head_ife.png31.94 KBberdir
ajax-option.png346.03 KBnghai

Comments

nghai created an issue. See original summary.

cilefen’s picture

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

berdir’s picture

Status: Active » Needs review
StatusFileSize
new31.94 KB
new23.59 KB
new25.3 KB
new22.22 KB
new2.1 KB

Proof of concept, this works, but not sure about the IEF part. Always the question where to put code that is integrating two modules, but right now, without that, you get duplicated messages and they are also quite challenging to theme inside the typical exposed form, not a lot of room. And forms tend to be short and typically on top of the page.

Also, the IFE thing also happens on non-ajax views, so that could be fixed separately from this. Didn't find an existing issue for views + IEF though.

Screenshots. No screenshot when ife is disabled, but then you simply get nothing until the refresh.

HEAD, with IFE enabled:

HEAD, after page refresh (with and without IFE)

With patch, without the IFE exclude, also HEAD without ajax

With patch including IFE exclude.

lendude’s picture

While printing the messages makes sense to me, I'm just curious how it looks when there are existing messages on the page and you get a new error message. Can we somehow use the Javascript Messaging API #77245: Provide a common API for displaying JavaScript messages ?

The IFE fix feel a bit like a band-aid, it seems that IFE + AJAX is not 100% (just getting this from the comments by @dmsmidt in the above issue, didn't do any research to verify this). But as a band-aid it makes sense...

berdir’s picture

I don't feel like this is the issue to start using that API? I just replicated the existing default ajax behavior, when you only return a return array then messages are automatically prefixed, exactly like this. See \Drupal\Core\Render\MainContent\AjaxRenderer::renderResponse(). It's only when you return an ajax response that you need to take care of that yourself.

Yes, the IFE exclude is certainly a band-aid, but ajax is not the only problem. The problem is that views exposed forms have this really weird behavior where they store label information in a separate structure and only set them in pre_render on the form elements, then IFE sees no label and bails out in \Drupal\inline_form_errors\FormErrorHandler::displayErrorMessages(). If the label would be set, you would instead see the usual "2 errors found on: LabelA, LabelB" message on top, not the duplicated validation error.

And IMHO even if that would work, it wouldn't make that much sense in this context, so I think opting out makes sense. Then again, I proposed the same for the login form which is IMHO weird with IFE, but wasn't able to convince others :)

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

agileadam’s picture

For what it's worth, Berdir's patch in #5 is working well for me on 8.7.11.

hardik_patel_12’s picture

StatusFileSize
new2.1 KB

Re-rolled for D8.9.x

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

kishor_kolekar’s picture

StatusFileSize
new3.78 KB

I've re-rolled patch for 9.1

acbramley’s picture

Status: Needs review » Needs work
Issue tags: -views, -Ajax, -exposed filter, -form error, -validation +Needs tests

Works really well, just needs tests! I'll look to add some tomorrow.

acbramley’s picture

Assigned: Unassigned » acbramley

Working on tests

acbramley’s picture

Assigned: acbramley » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new6.19 KB
new2.4 KB

Here's #12 with a test and test-only patch (also the interdiff)

acbramley’s picture

Issue tags: +Bug Smash Initiative

Status: Needs review » Needs work

The last submitted patch, 15: 2977785-15-test-only.patch, failed testing. View results

amjad1233’s picture

  1. +++ b/core/modules/views/src/Controller/ViewAjaxController.php
    @@ -200,6 +201,14 @@ public function ajaxView(Request $request) {
    +          $response->addCommand(new PrependCommand(".js-view-dom-id-$dom_id", $output));
    

    Micro-optimisation:

    Was wondering if sprintf() can be used to add $dom_id

acbramley’s picture

Status: Needs work » Needs review

Test-only failed as expected. Back to NR

sam152’s picture

Looks great, just a few questions re: unrelated changes.

  1. +++ b/core/modules/views/src/Controller/ViewAjaxController.php
    @@ -136,17 +137,17 @@ public function ajaxView(Request $request) {
           foreach ([
    -          'view_name',
    -          'view_display_id',
    -          'view_args',
    -          'view_path',
    -          'view_dom_id',
    -          'pager_element',
    -          'view_base_path',
    -          AjaxResponseSubscriber::AJAX_REQUEST_PARAMETER,
    -          FormBuilderInterface::AJAX_FORM_REQUEST,
    -          MainContentViewSubscriber::WRAPPER_FORMAT,
    -        ] as $key) {
    +        'view_name',
    +        'view_display_id',
    +        'view_args',
    +        'view_path',
    +        'view_dom_id',
    +        'pager_element',
    +        'view_base_path',
    +        AjaxResponseSubscriber::AJAX_REQUEST_PARAMETER,
    +        FormBuilderInterface::AJAX_FORM_REQUEST,
    +        MainContentViewSubscriber::WRAPPER_FORMAT,
    +      ] as $key) {
    

    Formatting changes?

  2. +++ b/core/modules/views/src/Form/ViewsExposedForm.php
    @@ -34,7 +34,7 @@ class ViewsExposedForm extends FormBase {
    -   * Constructs a new ViewsExposedForm
    +   * Constructs a new ViewsExposedForm.
    
    @@ -86,7 +86,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
    -    // Check if the form was already created
    +    // Check if the form was already created.
    

    Maybe also out of scope?

acbramley’s picture

StatusFileSize
new4.5 KB
new1.99 KB

Yeah let's remove those changes.

sam152’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/core/modules/views/src/Form/ViewsExposedForm.php
@@ -145,6 +145,9 @@ public function buildForm(array $form, FormStateInterface $form_state) {
+    // Labels are built too late for inline form errors to work, resulting
+    // in duplicated messages.
+    $form['#disable_inline_form_errors'] = TRUE;

Looks good to me. I've read @Berdir's comment on opting out here. Not sure how it was managed elsewhere, but it seems fine that if someone needed this feature a follow-up could be opened to address it.

acbramley’s picture

StatusFileSize
new4.49 KB

Here's the 8.9 patch.

lendude’s picture

  1. +++ b/core/modules/views/src/Form/ViewsExposedForm.php
    @@ -149,6 +149,9 @@ public function buildForm(array $form, FormStateInterface $form_state) {
    +    // Labels are built too late for inline form errors to work, resulting
    +    // in duplicated messages.
    +    $form['#disable_inline_form_errors'] = TRUE;
    

    Do we need test coverage for this?

  2. +++ b/core/modules/views/tests/src/FunctionalJavascript/ExposedFilterAJAXTest.php
    @@ -151,4 +165,17 @@ public function testExposedFiltersInModal() {
    +    $this->assertSession()->assertWaitOnAjaxRequest();
    

    Can we use something other then assertWaitOnAjaxRequest()? Seems like we can use waitForElement here which is less brittle.

jibran’s picture

Status: Reviewed & tested by the community » Needs work

How can we test #24.1?
I agree #24.2 can be changed.

+++ b/core/modules/views/tests/src/FunctionalJavascript/ExposedFilterAJAXTest.php
@@ -151,4 +165,17 @@ public function testExposedFiltersInModal() {
+    $this->assertSession()->assertWaitOnAjaxRequest();
+    $this->assertSession()->elementExists('css', 'div[aria-label="Error message"]');

These two can be combined and waitForElement('div[aria-label="Error message"]') can be used.

deepak goyal’s picture

Status: Needs work » Needs review
StatusFileSize
new4.88 KB
new1.21 KB

Hi @jibran
Updated patch please review.

jibran’s picture

Thanks!

+++ b/core/modules/views/tests/src/FunctionalJavascript/ExposedFilterAJAXTest.php
@@ -123,7 +137,7 @@ public function testExposedFiltersInModal() {
-    $this->assertSession()->assertWaitOnAjaxRequest();
+    $this->assertSession()->waitForElement();

Please revert this change.

jibran’s picture

On second thought, I don't think we need tests for #24.1.

Status: Needs review » Needs work

The last submitted patch, 26: 2977785-26.patch, failed testing. View results

mrinalini9’s picture

Status: Needs work » Needs review
StatusFileSize
new4.44 KB
new728 bytes

Reverted changes from patch #26 as mentioned in #27, please review.

Status: Needs review » Needs work

The last submitted patch, 30: 2977785-30.patch, failed testing. View results

mrinalini9’s picture

Status: Needs work » Needs review
StatusFileSize
new4.45 KB
new788 bytes

Fixing test case failure issue in #30, please review.

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Great!

quietone’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update

I read the Issue Summary and skimmed the issue and the patch. I think the proposed resolution in the IS should match the solution in the patch, and it doesn't seem like it does. The IS contains a before screenshot, does it need an after screenshot? The IS says there is an issue for Drupal 7, is there any recommendation here for helping to finish that issue? Adding tag for updating the IS based on those questions.

And during my skim of the patch I notice 2 nits.

+++ b/core/modules/views/src/Controller/ViewAjaxController.php
@@ -22,6 +22,7 @@
+use Drupal\Core\Ajax\PrependCommand;

Can we add this in alphabetical order? There are other entries in the use statements using \Drupal\Core\Ajax\... and this would fit nicely there.

+++ b/core/modules/views/tests/src/FunctionalJavascript/ExposedFilterAJAXTest.php
@@ -19,13 +20,20 @@ class ExposedFilterAJAXTest extends WebDriverTestBase {
+  protected static $modules = ['node', 'views', 'views_test_modal', 'user_test_views'];

greater than 80 characters

hardik_patel_12’s picture

Status: Needs work » Needs review
StatusFileSize
new4.68 KB
new1.42 KB

Fixing 2 nits , use statements in alphabetical order and greater than 80 characters.

Status: Needs review » Needs work

The last submitted patch, 35: 2977785-35.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

hardik_patel_12’s picture

Status: Needs work » Needs review

Random failure back to need review.

snehalgaikwad’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new32.58 KB
new50.78 KB

According to the issue summary I reproduced this issue on local. Error message was not shown for exposed filter in view with AJAX. Patch #35 applied cleanly. Now error message is shown for exposed fields.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 35: 2977785-35.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

ravi.shankar’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new4.46 KB
new511 bytes

Here I have fixed a coding standard message of patch #35.

Looks like an unrelated test failure, so making it RTBC as per comment #38.

larowlan’s picture

On second thought, I don't think we need tests for #24.1.

Can you elaborate on what the reasoning was here - cheers

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 40: 2977785-40.patch, failed testing. View results

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kim.pepper’s picture

Issue tags: +#pnx-sprint

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

acbramley’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update
StatusFileSize
new4.93 KB
new1.49 KB

Added coverage for inline form errors, this fails without the disable_inline_form_errors line. Also fixed the expected message, added :void

acbramley’s picture

StatusFileSize
new4.93 KB
new695 bytes

It seems like all failures similar to #49 in core are already ignored so this is my best guess at something that will work for us.

Status: Needs review » Needs work

The last submitted patch, 50: 2977785-50.patch, failed testing. View results

sourabhjain’s picture

Assigned: Unassigned » sourabhjain
sourabhjain’s picture

Assigned: sourabhjain » Unassigned
Status: Needs work » Needs review
StatusFileSize
new1.23 KB
new4.27 KB

Fixed the error in #53 patch.
Please review.

tanuj.’s picture

StatusFileSize
new1.23 KB
new703 bytes

Tried to fix CCF on #53

lendude’s picture

Status: Needs review » Needs work

The new tests got lost in #53, please add them back in

prem suthar’s picture

StatusFileSize
new4.94 KB
new723 bytes

Add the Tests As Per #55 Suggestion. try To Fix failed test.

acbramley’s picture

Status: Needs work » Needs review
StatusFileSize
new5.63 KB
new537 bytes

The correct fix is to not let ViewAjaxControllerTest return NULL for renderRoot. There was already a mock for render which isn't even called in that test, so we just replace that mock with renderRoot

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Confirmed the issue following the steps in the issue summary.
Using patch #57 I now get an error.
There are no users matching "asfsd".

Changes look good.

larowlan’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/modules/views/src/Controller/ViewAjaxController.php
@@ -200,6 +201,14 @@ public function ajaxView(Request $request) {
+        $output = $this->renderer->renderRoot($status_messages);

Should we be using ::executeInRenderContext like we do on 193-201 just in case something leaks metadata?

ameymudras’s picture

Status: Needs review » Needs work
StatusFileSize
new5.78 KB
new892 bytes
ameymudras’s picture

Status: Needs work » Needs review
ameymudras’s picture

StatusFileSize
new5.79 KB
new815 bytes

Status: Needs review » Needs work

The last submitted patch, 62: 2977785-62.patch, failed testing. View results

acbramley’s picture

Status: Needs work » Needs review
+++ b/core/modules/views/src/Controller/ViewAjaxController.php
@@ -200,6 +201,17 @@ public function ajaxView(Request $request) {
+        $output = $this->renderer->executeInRenderContext(new RenderContext(), function () use ($renderer, $status_messages) {

I think we need to pop the context here and apply the cacheable metadata, but then what would we apply that to?

However, looking at other usages of status messages in AJAXy type scenarios in core we never use executeInRenderContext.

See AjaxRenderer, ManagedFile, ViewsFormBase

So I think we're good with #57.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Think I agree with @acbramley

If there's room for refactoring the standard of AJAX status messages maybe that should be a follow up?

The last submitted patch, 57: 2977785-57.patch, failed testing. View results

larowlan’s picture

Status: Reviewed & tested by the community » Needs review

ViewsFormBase is wrapped by \Drupal\views_ui\Form\Ajax\ViewsFormBase::ajaxFormWrapper as far as I can see, and that does the executeInRenderContext dance.

Status messages is placeholdered so in Big pipe scenarios could require JS, additionally anyone could in theory alter #cache/#attached in a hook_preprocess_status_messages.

However looking \Drupal\Core\Ajax\CommandWithAttachedAssetsTrait::getRenderedContent it looks like if you pass in an array it will take care of getting the attached libraries and rendering, so can we just pass ['#type' => 'status_messages'] as the content to the prepend command? Would simplify things a lot I think so it's worth a shot I think.

acbramley’s picture

StatusFileSize
new5.33 KB
new959 bytes

@larowlan that does work! So much cleaner.

Status: Needs review » Needs work

The last submitted patch, 68: 2977785-68.patch, failed testing. View results

acbramley’s picture

Status: Needs work » Needs review
StatusFileSize
new5.9 KB
new856 bytes

Need to mock getInfo on the element manager now since way down the stack that is called with the new method of passing in an array.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Retested following steps in issue summary.
Patch #70 still addresses the issue.

larowlan’s picture

Can we get an answer for #41 or alternatively @Lendude can you confirm they're not needed?

larowlan’s picture

Adding issue credits

lendude’s picture

I think we already have it when pageTextContains was changed to pageTextContainsOnce, basically what I was looking for was something that would fail if somebody removed $form['#disable_inline_form_errors'] = TRUE;, and if the comment above that line is correct, that should do the trick.

So this looks good to me, nice work all!

larowlan’s picture

  • larowlan committed 3afc1fc4 on 10.0.x
    Issue #2977785 by acbramley, ameymudras, mrinalini9, Hardik_Patel_12,...

  • larowlan committed bb8f1aad on 10.1.x
    Issue #2977785 by acbramley, ameymudras, mrinalini9, Hardik_Patel_12,...
larowlan’s picture

Title: No error messages are shown for applied validation on a view exposed filter with on "AJAX" » [Needs backport] No error messages are shown for applied validation on a view exposed filter with on "AJAX"
Version: 10.1.x-dev » 9.5.x-dev

Committed to 10.1.x and backported to 10.0.x

Queued a test run for 9.5.x, will backport there if it passes.

  • larowlan committed 5a2ddf59 on 9.5.x
    Issue #2977785 by acbramley, ameymudras, mrinalini9, Hardik_Patel_12,...
larowlan’s picture

Title: [Needs backport] No error messages are shown for applied validation on a view exposed filter with on "AJAX" » No error messages are shown for applied validation on a view exposed filter with on "AJAX"
Status: Reviewed & tested by the community » Fixed

9.5 run came back green, backported to 9.5.x - marking as fixed 💪

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

kostiantyn’s picture

Is there a patch available for drupal 8.9? I can't seem to find one. The one for version 9 isn't working for me.