0

I need to show a modal message like "Uploaded file exceeds X MB" when the user tries to upload a large file. I have set maximum size in the field settings and when I try to upload a large file Drupal shows a label with custom error. I need to trap this event and launch the js with modal message.

I have a;

 function hook_file_validate($file) {

     ....
 }

It is invoked in ajax so I cannot make a drupal_add_js call with the javascript code to execute. How can I execute JS method for solving this problem?

1 Answer 1

0

It sounds like you're wanting to do file size upload validation with complicated method of server/client code. If all you need is size validation, you can do that client-side without the need of the server.

There are many ways to do this; one example would involve adding a JS onchange event handler to your file field.

MYMODLE_form_alter(&$form, &$form_state, $form_id) {
  if($form_id = "my_form") {
    $form['field_MYFILEFIELD']['#attributes']['onchange'] = "
      if (event.srcElement.files[0].size > (8 * 1048576)) { 
        alert('Uploaded file exceeds 8 MB'); 
        jQuery(this).replaceWith(jQuery(this).clone()); 
        return false;  
      }";
  }
  //@TODO: move the validation code to a function & use better modal
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.