You are viewing limited content. For full access, please sign in.

Question

Question

Eliminating duplicate file names

asked on January 4, 2021

I have a form with a table that I need to ensure does not contain duplicate values.  The first field in the table is a drop down field.  I use the following script to ensure that the drop down values selected are not duplicated and it works great.

 

The second field is a file upload field.  I need to ensure that the file names aren't duplicated.  How can I modify this script to collect the file names and determine if there are duplicates.

Thanks!

$( document ).ready(function() {
 //*** BEGIN script to validate drop down field
// assign validator to initial rows
assignValidator();
// re-validate when a row is added or removed
  $('.myTable').on('click','.cf-table-add-row, .cf-table-delete',function(){
//alert('assignValidator when table row added')
    assignValidator();
  });
});

function assignValidator(){
  // add unique validator
  $('.myTable .myField select').attr('data-parsley-uniqueselection','');

  // trigger validation on all rows when a row changes
  $('.myTable .myField select').off('change',validateSelection).on('change',validateSelection);
}

function validateSelection(){
  // revalidate the column if the value is not empty

  $('.myTable .myField').each(function(e){
      if($(this).val() != ''){
        $(this).parsley().validate();
      }
  });
}
window.Parsley.addValidator('uniqueselection', {

validateString: function(value, requirement, field) {
// return a list of all column values
    var valueList = field.$element.closest('.cf-table').find('.myField select').map(
      function() {return $(this).val().trim(); }).get();

    // return the result of a filter for more than 1 match for the current field value
    return _.filter(valueList, function(v) { return v == value }).length == 1;
  },

  messages: {

    en: 'Duplicate selection.',

  }
  //*** End script to validate drop down field
//end document ready function  
});

 

0 0

Replies

replied on January 7, 2021

@████████ - Would you be able to look at this for me.  Thanks!

0 0
replied on January 7, 2021

Hi Janice,

It just so happens I've done this for another process. Here is the code I used:

  window.Parsley.addValidator('uniquefilename', {
    validateString: function(value, requirement, field) {
      var nameField = field.$element.siblings('.files').find('.file .ellipsis');
      
      var fileName = nameField.length > 0 ? nameField.attr('title') : '';
      
      var valueList = field.$element.closest('.validate_uniquerow').find(
      		'.validate_uniquefile .files .file').map(
        		function() {
                  var listName = $(this).find('.ellipsis');
                  var name = listName.length > 0 ? listName.attr('title') : '';                
                  return (name.trim());
            }).get();
      
      // add check for file sizes
      return _.filter(valueList, function(v) {
        return (v == fileName.trim());
      }).length == 1;
    },
    messages: {
      en: 'Duplicate file name.',
    }
  });

In this example, I have the class "validate_uniquerow" set for the table and "validate_uniquefile" set on the file upload field.

The .length checks are to account for rows that have no file yet; it's been awhile, but I think the ellipsis container doesn't exist when there's no file uploaded.

0 0
replied on January 7, 2021

Thank you so much, Jason.  I will give it a try.

-Janice

0 0
replied on January 7, 2021

Just a note, my version was comparing both file name and file size. I stripped out the file size piece and I think I got everything, but if it doesn't work check the console for errors since I may have missed a semicolon or bracket or something.

0 0
replied on January 14, 2021

Thanks!  I haven't gotten it to work yet but I also haven't been able to do a in-depth troubleshooting either.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.