To duplicate the issue:

* Create a managed_file element in a form
* Try to upload a file that will result in an error (i.e. with a not allowed extension)
* Now there is a nice error message just before the upload form element
* Upload a valid file.
* The error message produced from the previous attempt is still there.

Here is a possible solution (sorry can't create a patch now):
file.module lines 268-285 current code:

  // Add the special AJAX class if a new file was added.
  if (isset($form['#file_upload_delta']) && $current_file_count < $form['#file_upload_delta']) {
    $form[$current_file_count]['#attributes']['class'][] = 'ajax-new-content';
  }
  // Otherwise just add the new content class on a placeholder.
  else {
    $form['#suffix'] .= '<span class="ajax-new-content"></span>';
  }

  $output = theme('status_messages') . drupal_render($form) ;
  $js = drupal_add_js();
  $settings = call_user_func_array('array_merge_recursive', $js['settings']['data']);

  $commands = array();
  $commands[] = ajax_command_replace(NULL, $output, $settings);
  return array('#type' => 'ajax', '#commands' => $commands);

proposed modification:

  // Add the special AJAX class if a new file was added.
  if (isset($form['#file_upload_delta']) && $current_file_count < $form['#file_upload_delta']) {
    $form[$current_file_count]['#attributes']['class'][] = 'ajax-new-content';
  }
  // Otherwise just add the new content class on a placeholder.
  else {
    $form['#suffix'] .= '<span class="ajax-new-content"></span>';
  }

  $output = "<div class='file-upload-js-error'>" . theme('status_messages') . "</div>" . drupal_render($form) ; // we wrap the error message in a div
  $js = drupal_add_js();
  $settings = call_user_func_array('array_merge_recursive', $js['settings']['data']);

  $commands = array();
  $commands[] = ajax_command_remove(".file-upload-js-error"); // we remove a possible error message first
  $commands[] = ajax_command_replace(NULL, $output, $settings);
  return array('#type' => 'ajax', '#commands' => $commands);
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

pembeci’s picture

Version: 7.0 » 7.x-dev
Priority: Minor » Normal
pembeci’s picture

Title: Form element managed_file doesn't clear previous error message after successful ajax upload » File ajax upload doesn't clear previous error message after successful ajax upload
FileSize
742 bytes

I was on a Windows machine, finally found the time connecting a Linux and produced the patch.

Status: Needs review » Needs work

The last submitted patch, file_ajax_upload.patch, failed testing.

pembeci’s picture

Component: forms system » file system
Status: Needs work » Needs review
pembeci’s picture

FileSize
766 bytes

Can't apply patch. Path problem? Trying again.

bambilicious’s picture

I've reproduced this bug and the patch works perfect for me.
However, as the new strings introduced in #5 does not contains any variables or escape-sequences, I've changed "..." to '...'.

Please correct me if I'm wrong.

Status: Needs review » Needs work

The last submitted patch, 1020916-file_ajax_upload-2.patch, failed testing.

bambilicious’s picture

Status: Needs work » Needs review
FileSize
789 bytes

p0 format was used in previous patch, here's p1.

phreaknerd’s picture

Here is an alternative approach. It additionally prevents the file field to infinitely nest itself in a new div-container on each ajax request.

phreaknerd’s picture

Bump: Can anyone review those two lines of code, please? ;-)

phreaknerd’s picture

texas-bronius’s picture

Status: Needs review » Reviewed & tested by the community

Just chiming in:

  • Patch works great from my perspective! I'd just recommend not removing that one empty line (file.module:282) so as to keep with the original structure
  • Issue also noticeable when successive uploads fail: the error messages continue to stack. This patch successfully replaces it.

Am I allowed to mark RTBC? I'm gonna mark it... Thanks for sharing this simple fix!

phreaknerd’s picture

amateescu’s picture

Version: 7.x-dev » 8.x-dev
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Bugs are fixed in the latest development branch, and we also need an automated test here.

The test behavior should be pretty straightforward: upload a file that's not allowed, check that the error is present, upload a file that is allowed, check that the error is gone.

It's usually easier to find an existing test for the form element and stick the additional assertion in there. For example, assuming that uploading a file that is not allowed is already tested, you could add the new upload and assert just below that.

If the bug cannot be reproduced in the latest 8.x version, feel free to bump it back to 7.x :)

David_Rothstein’s picture

Status: Needs work » Closed (duplicate)

Although this issue is older, #1792032: File validation error message not removed after subsequent upload of valid file was already committed to Drupal 8 and also has a test.