1

I have a webform where users can upload one pdf file. I'd like to have this pdf file always the same name foo.pdf.

To avoid conflicts I save each upload to a different directory with the pattern foo/[random:hash:md5].

When trying to save these settings I get the error message To create unique file names, use at least one token in the file name pattern. I understand that this error makes sense to avoid naming conflicts but the conflicts should be avoided with the custom directory.

Is there a way to bypass the validation check?

Edit: For clarification. I need to bypass the validation check in the settings page for the field of the file upload not the validation for the form submission itself

Settings as I'd like to have them

  • Didn't you mean that you would use /[random:hash:md5]/foo.pdf as destination? You could unset the validation function, and perform all other Webform checks in your custom code, but I'm not sure how much sense does that make. – Aram Boyajyan Mar 18 '16 at 10:01
  • Clarification: I need to bypass the validation at the settings page not the form submission itself. I added a screenshot which reflect my wanted changes. – FeBe Mar 18 '16 at 10:13
  • Yes, I do understand that - you should be able to override that validation as well. I'll have a look a bit later if you don't figure it out meanwhile. – Aram Boyajyan Mar 18 '16 at 11:13
1

By looking at the source in /components/file.inc (in Webform module), you will see that the Webform declares a custom element validation callback that performs the check you want to avoid. In order to override this, you will have to hook into the settings form, and provide your own callback for this element.

Below is the code you would use to achieve that. Pay attention to the comments.

function YOUR_MODULE_form_webform_component_edit_form_alter(&$form, &$form_state) {
  // Override the element validation callback for renaming the file uploads.
  if (isset($form['extra']['rename'])) {
    // Loop through the validation callbacks and remove the one we want to
    // override. This way we will not affect any other module that alters this
    // part of the form.
    foreach ($form['extra']['rename']['#element_validate'] as $key => $validation_callback) {
      if ($validation_callback == '_webform_edit_file_rename_validate') {
        unset($form['extra']['rename']['#element_validate'][$key]);
      }
    }
    // Note: since the original validation callback provided in Webform module
    // only makes sure that the file names are unique, you don't need to provide
    // your own element validation callback. However, I would recommend doing so
    // just in case you have other webform nodes that will need this validation.
    //
    // Add our custom element validation callback.
    $form['extra']['rename']['#element_validate'][] = 'YOUR_MODULE_edit_file_rename_validate';
  }
}


function YOUR_MODULE_edit_file_rename_validate($element, &$form_state, $form) {
  // I recommend still limiting this only to specific nodes, content types, and
  // not disabling completely.
  if ($form['#node']->nid != YOUR_NODE_ID) {
    $rename = trim($form_state['values']['extra']['rename']);
    form_set_value($element, $rename, $form_state);
    if (strlen($rename) && !count(token_scan($rename))) {
      form_error($element, t('To create unique file names, use at least one token in the file name pattern.'));
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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