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

Question

Question

Don't allow duplication of collection

asked on May 3, 2021

I have a collection that the first field fills from a lookup.

A couple more of the fields of the collection fill when one of the lookups is selected from the first field.

I don't want the user to be able to select the same firs field value for the additional collection rows. 

 

After the add I don't want the same device s/n to be chosen again.

 

I found where this can be prevented in a table, but how do I do it in a collection?

 

0 0

Answer

SELECTED ANSWER
replied on May 3, 2021 Show version history

Here's an example, just update the classes and message as needed. This is only set up to work with rows that exist when the form is loaded, and rows that are manually added by the user.

If you have rows added by a lookup, then you'll need to set the validator attribute on lookup complete as well to ensure those rows a get the same validation.

$(document).ready(function(){
  // set validation for any sets that exist on form load
  $('.uniqueValue input').attr('data-parsley-uniquevalue','');
  
  // set validation when new rows are added
  $('.myCollection .cf-collection-append').click(function(){
    $('.uniqueValue input').attr('data-parsley-uniquevalue','');
  });
});

window.Parsley.addValidator('uniquevalue', {
  validateString: function(value, requirement, field) {
    var valueList = $('.uniqueValue').find('input:visible').map(
      function() {return $(this).val().trim(); }).get();
    return _.filter(valueList, function(v) { return v == value }).length == 1;
  },
  messages: {
    en: 'Value must be unique.',
  }
});

1 0
replied on May 4, 2021

Thank you so much.  

0 0

Replies

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

Sign in to reply to this post.