Requiring a ticket attachment if a particular dropdown option is selected



已于 2019年3月27日 发布

Update from Community Team: jQuery has removed in v2 themes and this tip doesn't work without jQuery.

 

How to require an attachment in a ticket if a particular dropdown option is selected

We needed to require an attachment if a certain dropdown option was selected by the user when they are filling out a certain form to submit a ticket. Without being able to require the attachment, extra time would need to be spent by the agent to contact the user and ask for the additional document. 

Solution Overview

This was done by creating a custom checkbox field that is set to be required, and adding the checkbox to the form, hiding the checkbox via CSS, then programatically selecting or clearing the checkbox according to the dropdown option selected and the number of attachments present.

Limitations 

  • This solution detects that an attachment has been uploaded, but doesn't verify that the attachment is the particular document that needs to be attached.
  • This solution uses MutationObserver (https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/MutationObserver), which is not supported in some older browser versions
  • A user could get around the requirement to upload an attachment by using the DOM inspector to show the checkbox, and then selecting it manually. 

Steps

Add the custom checkbox field

Create the custom checkbox field

  1. Navigate to the Ticket Fields page
  2. Click Add field
  3. Select Checkbox type
  4. Under Permissions, select Editable for end users
  5. Enter whatever title you would like, in Title shown to agents and Title shown to end users. I used the title 'Attachment'
  6. Select the Required to submit a request option.
  7. Click Save to create the field
  8. Note the field ID shown in the list of ticket fields for the new ticket field

Add the custom checkbox field to the form

  1. Navigate to the Ticket Fields page
  2. Locate the form for which you need to require the attachment, and click it
  3. Drag the new field from the list of fields on the right to the list of fields in the form. Make sure that it is the last field in the form (because you want the error notification that appears if an attachment is required but not uploaded, to appear above the attachment upload box).
  4. Click Save form to save the change.

Hide the checkbox field

Add the following to style.css to hide the field label and the field (replace the custom field ID in this code with your custom field ID). You have to hide the field label and field individually instead of hiding the <div> that contains them, because if there is an error notification that an attachment wasn't uploaded, it is inside the <div>.

#request_custom_fields_360017775731_label,
#request_custom_fields_360017775731 {
display: none;
}

Set the checkbox according to the attachments and dropdown

1) Determine the field ID of the dropdown. You can do this from the Ticket Fields page or with the DOM inspector by opening the form in your Zendesk, and then inspecting the dropdown. The screenshot below shows the dropdown on the web page, and the element inspector portion that shows the field ID

2) Add the following code in script.js inside the document ready function, e.g.after $(document).ready(function () { :  and before the ending line }); [You have to adjust the field IDs in attachmentCheckboxField and formDropdownClass to match your field IDs, and adjust the option checked in function isFormAttachmentRequired to match the particular option in your case]:

// Function to start observing node for mutations
var startObserveMutations = function (nodeSelector, options, callbackFunction) {
var node = document.querySelector(nodeSelector);
if (node) {
   var observer = new MutationObserver(callbackFunction);
   observer.observe(node, options);
  return observer;
  }
};

// Callback function to execute when mutations in form attachments or dropdown are observed: 
// clear or select Attachment checkbox according to dropdown
var mutationObservedForm = function (mutationsList) {
  mutationsList.forEach(function (mutation) {
    if (mutation.type == 'childList') {
      setFormAttachmentCheckbox();
    }
  });
};

// Define some variables for requiring form attachments
var attachmentCheckboxField = 'request_custom_fields_360017775731';
var attachmentCheckboxId = '#' + attachmentCheckboxField;
var attachmentErrorNotification = 'Report must be attached';
var formDropdownClass = '.request_custom_fields_360017548611';
var formObserveMutationOptions = { childList: true, subtree: true };

// Clear or select checkbox according to dropdown and attachments:
// Set Attachment checkbox if no attachments required, or if attachments are required and at least one is uploaded, otherwise clear it
function setFormAttachmentCheckbox() {
  if (isFormAttachmentRequired()) {
    if ($('#request-attachments-pool .upload-item').length) {
      selectCheckbox(attachmentCheckboxId);
    }
    else {
      clearCheckbox(attachmentCheckboxId);
    }
  }
  else {
    selectCheckbox(attachmentCheckboxId);
  }
}

// Return true if dropdown option 'ABCD' is selected
function isFormAttachmentRequired() {
  return $(formDropdownClass + ' a.nesty-input').attr('aria-expanded') &&
         $(formDropdownClass + ' a.nesty-input').text() === 'ABCD';
}

// Select checkbox
function selectCheckbox(eltselector) {
  $(eltselector).prop('checked', true);
}

// Clear checkbox
function clearCheckbox(eltselector) {
  $(eltselector).prop('checked', false);
}

// If attachment checkbox field exists:
//   Select the checkbox if attachment is not required
//   Watch for changes to attachments and dropdown
if ($(attachmentCheckboxId).length) {
  if (!isFormAttachmentRequired()) {
    selectCheckbox(attachmentCheckboxId);
  }
  startObserveMutations('#request-attachments-pool', formObserveMutationOptions, mutationObservedForm);
  startObserveMutations(formDropdownClass, formObserveMutationOptions, mutationObservedForm);
}

// Adjust attachment error notification
var attachmentErrorElt = $('.' + attachmentCheckboxField + ' .notification-error');
if (attachmentErrorElt.length) {
  attachmentErrorElt.text(attachmentErrorNotification);
}

Note: The code that detects changes to the attachments and dropdown uses MutationObserver (https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/MutationObserver), as mentioned above. Basically you create an observer instance and pass it a callback function to execute when changes are observed. Then you start the observer, passing it the target node and options specifying what changes to observe.

When the above steps are done, and style.css and script.js are changed as shown above, then you will see that if you open the form and select the dropdown option that requires an attachment, and then click Submit, an error notification is displayed:

Hope this helps somebody!


0

61

61 条评论

6902233512986  yes I swapped out the XXXX's for my field ID.

0


4513632515226 Did you swap out the XXXXX for your field ID? 
e.g.:


var attachmentCheckboxField = 'request_custom_fields_XXXXXX';
var attachmentCheckboxId = '#' + attachmentCheckboxField;
var attachmentErrorNotification = 'User request template must be attached';
var formDropdownClass = '.request_custom_fields_XXXXXX';

 

#request_custom_fields_XXXXXX_label,
#request_custom_fields_XXXXXX {

 

0


6902233512986 your code seems to be missing the piece where the attachment is required IF a specific option is selected in a field. I believe all the people who are having issues are people who already added the attachment field and hid it. But the requirement to have the attachment is not working when a specific option is selected. This code actually worked for me for about a year and some time inbetween something changed and is no longer working.

1263213537349 wondering if you can help. I added the codes to the style.css and the script.js and it's still showing the attachment as optional. I also have two other forms that would require attachments and those are not working as well. I'm On Copenhagen version 2.16.1 Templating API v2. Any help would be great! Thanks!


 Added jQuery to document_head.hbs

 

Selection to trigger attachment requirement

Result. The “Attachment” field is still listed as optional

Entered in style.css 

#request_custom_fields_XXXXXX_label,
#request_custom_fields_XXXXXX {
display: none;
}

 

Entered in script.js

// REQUIRE ATTACHMENTS ON ABC FORM 
$(document).ready(function() {
  // Function to start observing node for mutations
var startObserveMutations = function (nodeSelector, options, callbackFunction) {
var node = document.querySelector(nodeSelector);
if (node) {
   var observer = new MutationObserver(callbackFunction);
   observer.observe(node, options);
  return observer;
  }
};

// Callback function to execute when mutations in form attachments or dropdown are observed: 
// clear or select Attachment checkbox according to dropdown
var mutationObservedForm = function (mutationsList) {
  mutationsList.forEach(function (mutation) {
    if (mutation.type == 'childList') {
      setFormAttachmentCheckbox();
    }
  });
};

// Define some variables for requiring form attachments
var attachmentCheckboxField = 'request_custom_fields_XXXXXX';
var attachmentCheckboxId = '#' + attachmentCheckboxField;
var attachmentErrorNotification = 'User request template must be attached';
var formDropdownClass = '.request_custom_fields_XXXXXX';
var formObserveMutationOptions = { childList: true, subtree: true };

// Clear or select checkbox according to dropdown and attachments:
// Set Attachment checkbox if no attachments required, or if attachments are required and at least one is uploaded, otherwise clear it
function setFormAttachmentCheckbox() {
  if (isFormAttachmentRequired()) {
    if ($('#request-attachments-pool .upload-item').length) {
      selectCheckbox(attachmentCheckboxId);
    }
    else {
      clearCheckbox(attachmentCheckboxId);
    }
  }
  else {
    selectCheckbox(attachmentCheckboxId);
  }
}

// Return true if dropdown option 'Documentation' is selected
function isFormAttachmentRequired() {
// Get the text of the selected option in the dropdown
  var selectedOptionText = $(formDropdownClass + ' a.nesty-input').text();
  return $(formDropdownClass + ' a.nesty-input').attr('aria-expanded') &&
         (selectedOptionText === 'Documentation';
}

// Select checkbox
function selectCheckbox(eltselector) {
  $(eltselector).prop('checked', true);
}

// Clear checkbox
function clearCheckbox(eltselector) {
  $(eltselector).prop('checked', false);
}

// If attachment checkbox field exists:
//   Select the checkbox if attachment is not required
//   Watch for changes to attachments and dropdown
if ($(attachmentCheckboxId).length) {  
  if (!isFormAttachmentRequired(formAttachmentDropdownClass)) {
    selectCheckbox(attachmentCheckboxId);
  }
  startObserveMutations('#request-attachments-pool', formObserveMutationOptions, mutationObservedForm);
  startObserveMutations(formDropdownClass, formObserveMutationOptions, mutationObservedForm);
}

// Adjust attachment error notification
var attachmentErrorElt = $('.' + attachmentCheckboxField + ' .notification-error');
if (attachmentErrorElt.length) {
  attachmentErrorElt.text(attachmentErrorNotification);
}
 
})

0


I got this code working for my instance (Copenhagen theme). Added to script.js.

You need to create a check box (call it whatever you want. I called mine Attachments) and make it mandatory to submit a ticket.

 

Add the Checkbox to the form you want to use at the very bottom. Copy the ID for both the drop down menu where you need attachments and check box and remove the X's with the corresponding IDs.

 // Can't submit damage ticket without attachment
var startObserveMutations = function(nodeSelector, options, callbackFunction) {
 var node = document.querySelector(nodeSelector);
 if (node) {
   var observer = new MutationObserver(callbackFunction);
   observer.observe(node, options);
   return observer;
 }
};
// Callback function to execute when mutations in form attachments are observed:
// clear or select Attachment checkbox according to attachments
var mutationObservedForm = function(mutationsList) {
 mutationsList.forEach(function(mutation) {
   if (mutation.type === 'childList') {
     setFormAttachmentCheckbox();
   }
 });
};
// Define some variables for requiring form attachments
var attachmentCheckboxField = 'request_custom_fields_XXXXXX';
var attachmentCheckboxId = '#' + attachmentCheckboxField;
var attachmentErrorNotification = 'Attachments cannot be blank';
var formDropdownClass = '.request_custom_fields_XXXXXX';
var formObserveMutationOptions = { childList: true, subtree: true };
// Clear or select checkbox according to attachments:
// Set Attachment checkbox if at least one attachment is uploaded, otherwise clear it
function setFormAttachmentCheckbox() {
 if ($('#request-attachments-pool .upload-item').length >= 1) {
   selectCheckbox(attachmentCheckboxId);
 } else {
   clearCheckbox(attachmentCheckboxId);
 }
}
// Select checkbox
function selectCheckbox(eltselector) {
 $(eltselector).prop('checked', true);
}
// Clear checkbox
function clearCheckbox(eltselector) {
 $(eltselector).prop('checked', false);
}
// If attachment checkbox field exists:
//   Watch for changes to attachments
if ($(attachmentCheckboxId).length) {
 startObserveMutations('#request-attachments-pool', formObserveMutationOptions, mutationObservedForm);
}
// Adjust attachment error notification
var attachmentErrorElt = $(attachmentCheckboxId + ' .notification-error');
if (attachmentErrorElt.length) {
 attachmentErrorElt.text(attachmentErrorNotification);
}
// Initial checkbox setting
setFormAttachmentCheckbox();    

Lastly, add this to style.css hide the check box field in the form. Replacing the X's with the checkbox ID

 

#request_custom_fields_XXXXXX_label,
#request_custom_fields_XXXXXX {
display: none;
}

0


To extend the functionality of the given code snippet to add an error notification for another dropdown option, we will modify the `isFormAttachmentRequired` function to check for the selection of the additional option as well. Assuming the additional option you want to include is 'EFGH', the code can be updated as follows:


// Return true if dropdown option 'ABCD' or 'EFGH' is selected
function isFormAttachmentRequired() {
 // Get the text of the selected option in the dropdown
 var selectedOptionText = $(formDropdownClass + ' a.nesty-input').text();
 // Check if the dropdown is expanded and if the selected option is 'ABCD' or 'EFGH'
 return $(formDropdownClass + ' a.nesty-input').attr('aria-expanded') &&
        (selectedOptionText === 'ABCD' || selectedOptionText === 'EFGH');
}

 

This code modification does the following:
- Retrieves the text of the currently selected option in the dropdown using jQuery.
- Checks if the dropdown is expanded (using the `aria-expanded` attribute) and if the selected option text matches either 'ABCD' or 'EFGH'.

With this change, the `isFormAttachmentRequired` function will now return `true` if either 'ABCD' or 'EFGH' is selected, which can then be used to trigger the mandatory field error code for these options.

Remember to replace `'ABCD'` and `'EFGH'` with the actual text of the options you are interested in, and ensure that `formDropdownClass` correctly represents the class of your dropdown element in the HTML structure of your Zendesk form.

0


Can you provide me the code to add the error notification for another option as well.

// Return true if dropdown option 'ABCD' is selected
function isFormAttachmentRequired() {
  return $(formDropdownClass + ' a.nesty-input').attr('aria-expanded') &&
         $(formDropdownClass + ' a.nesty-input').text() !=='';
}

Like this for another option in the same dropdown I need to add this Mandatory field error code

please help 6201945912474 4513632515226

0


 

Hello everyone,

I followed the procedure but my code does not work. I am a beginner and I would like to make it mandatory to add the attachment to send the form. Attached is my script:

$(document).ready(function () {
    
  // Function to start observing node for mutations
var startObserveMutations = function (nodeSelector, options, callbackFunction) {
var node = document.querySelector(nodeSelector);
if (node) {
   var observer = new MutationObserver(callbackFunction);
   observer.observe(node, options);
  return observer;
  }
};

// Callback function to execute when mutations in form attachments or dropdown are observed: 
// clear or select Attachment checkbox according to dropdown
var mutationObservedForm = function (mutationsList) {
  mutationsList.forEach(function (mutation) {
    if (mutation.type == 'childList') {
      setFormAttachmentCheckbox();
    }
  })};
          
// Define some variables for requiring form attachments
var attachmentCheckboxField = 'request_custom_fields_15233785880850';
var attachmentCheckboxId = '#' + attachmentCheckboxField;
var attachmentErrorNotification = 'Test';
var formDropdownClass = '.request_custom_fields_15106539093266';
var formObserveMutationOptions = { childList: true, subtree: true };

// Clear or select checkbox according to dropdown and attachments:
// Set Attachment checkbox if no attachments required, or if attachments are required and at least one is uploaded, otherwise clear it
function setFormAttachmentCheckbox() {
  if (isFormAttachmentRequired()) {
    if ($('#request-attachments-pool .upload-item').length) {
      selectCheckbox(attachmentCheckboxId);
    }
    else {
      clearCheckbox(attachmentCheckboxId);
    }
  }
  else {
    selectCheckbox(attachmentCheckboxId);
  }
}

// Return true if dropdown option 'ABCD' is selected
function isFormAttachmentRequired() {
  return $(formDropdownClass + ' a.nesty-input').attr('aria-expanded') &&
         $(formDropdownClass + ' a.nesty-input').text() !=='';
}

// Select checkbox
function selectCheckbox(eltselector) {
  $(eltselector).prop('checked', true);
}

// Clear checkbox
function clearCheckbox(eltselector) {
  $(eltselector).prop('checked', false);
}

// If attachment checkbox field exists:
//   Select the checkbox if attachment is not required
//   Watch for changes to attachments and dropdown
if ($(attachmentCheckboxId).length) {
  if (!isFormAttachmentRequired(formAttachmentDropdownClass)) {
    selectCheckbox(attachmentCheckboxId);
  }
  startObserveMutations('#request-attachments-pool', formObserveMutationOptions, mutationObservedForm);
  startObserveMutations(formDropdownClass, formObserveMutationOptions, mutationObservedForm);
}

// Adjust attachment error notification
var attachmentErrorElt = $('.' + attachmentCheckboxField + ' .notification-error');
if (attachmentErrorElt.length) {
  attachmentErrorElt.text(attachmentErrorNotification);
}

Can you help me ?

0


Here's a simpler way to enforce that users upload an attachment before submitting a request. These changes should be added to the script.js. 

1. Disable the submit button on the form you wish to enforce attachment upload.

const button = document.querySelector('[value="Submit"]');
button.disabled = true;

2. Change the attachments default label to let the user know an attachment is required.

$('label[for="request-attachments"]').html('<b>Attach file template to proceed</b>.');  

3. Add an event listener to the attachments area to detect changes, and only enable the submit button (and change warning text) if there is at least one file attached.

 document.getElementById('request-attachments').addEventListener('change', function(event) {
  var fileInput = event.target;
  var fileCount = fileInput.files.length;
   if (fileCount > 0) {
     attachmentLabel.text("Attachments"); 
     button.disabled = false;
   }
  });

Here's how it looks on the submit a request page, before and after attachment upload:

Before:

After:

Hope this helps someone else!

3


I'm sorry, but I can't believe this is not a standard feature available via forms.

This is such a basic use case which thousands of customers will require, and we have to build some complicated, messy workaround on our own in order to realize it.

It baffles me how expensive this tool is for the amount of features it offers.

1


Hi Zendesk Community,

I did as explained: 

Made the checkbox mandatory and filled the 3 variables in the code (checkbox ID, dropdown ID and i kept abcd as a value) and for any other value than abcd i keep getting "checkbox checked required" error message..

I get that;

0


登录再写评论。

找不到所需的内容?

新建帖子