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
});