In form fields, is it possible to enforce that ranking numbers are unique? For example, if the user enters the number '1' as a selection, they cannot enter that number again.
Question
Question
Replies
Yes, if you're on at least Forms 10.2 you can create a custom Parlsley validator to enforce uniqueness on the fields. The exact implementation really depends on what kind of fields you're using. For example, if you're using a table or collection the user can add rows to, then there are extra steps to take.
By using a custom Parsley validator, it would behave similar to the built-in validations (i.e., highlighting bad inputs in red and preventing form submission).
The following code creates a validator called "uniquevalue" that looks at all fields with the "uniqueField" class. If it finds another field with the same value as the current field, it flags it as invalid and shows the "Field value must be unique" message.
window.Parsley.addValidator('uniquevalue', { validateString: function(value, requirement, field) { var valueList = $('.uniqueField').find('input:visible').map( function() {return $(this).val().trim(); }).get(); return _.filter(valueList, function(v) { return v == value }).length == 1; }, messages: { en: 'Field value must be unique.', } });
NOTE: The code above is only validating against visible fields, so if you have fields hidden by field rules it would not count them. To remove that, just change 'input:visible' to 'input' on the 3rd line.
To assign the validator, you just do the following:
$('.uniqueField input').attr('data-parsley-uniquevalue','');
However, if your fields are in a table/collection any rows added after the form loads would not have this validator, so you would need to run that second line of code any time new rows are added.
If rows could be added by the user, then you could attach an event handler to the table's add row button.
If you have lookups that can add rows, then you could do the same with the lookupcomplete event.