My site uses a CPT called "Voyages". When submitting a Voyage there are two options, we'll call these Option 1 and Option 2.
At the beginning of the CRED form a user will select one of those options, and as a result the fields are as follows...
Option 1 Fields
Embarkation Date
Embarkation Location
Disembarkation Date
Disembarkation Location
Option 2 Fields
Embarkation Date and Time (Repeating Field)
Voyage Length in Hours
Disembarkation Location
The Ideal Situation: One Archive page that displays all Voyages, sorted by Embarkation Date, with the ability to set filters to show voyages within a specific timeframe.
For maximum flexibility in customising Queries and Filters on the Archive page, it would be ideal if both options used the same repeating field "Embarkation Date and Time", however, when filling in the CRED form a user MUST NOT be able to add more than one value to the repeating field if they selected Option 1 earlier in the form.
I have so far found the following threads which apparently limit the number of repeat entries that can be made.
https://toolset.com/forums/topic/limit-mumbr-of-instances-of-multiple-repeating-fields/#post-241961
https://toolset.com/forums/topic/limit-number-uploading-files/#post-335393
https://toolset.com/forums/topic/how-to-use-javascript-in-cred-form-for-validation/#post-260891
What I imagine I need is a version of one of the above linked scripts that first checks the value entered in a select field earlier in the form and actions only if a specific value was selected. Can that be done?
These threads are too old to take as a reference, but this one still works:
https://toolset.com/forums/topic/limit-mumbr-of-instances-of-multiple-repeating-fields/#post-241961
You need to adjust the Field type (in my case a single line), and the length
jQuery(function( $ ) {
$('.js-wpt-repadd').blur(function( e ) {
var length = $('input[type="text"]', $(this).closest('.js-wpt-field-items')).length;
if(length>=5) {
$(this).hide();
}
SetDeleteEvent();
});
var SetDeleteEvent = function() {
$('.js-wpt-repdelete').each(function(index, element) {
if (!$(element).data('bound')) {
$(element).click(function(e) {
var length = $('input[type="text"]', $(this).parent()).length;
if(length<=5) {
$('.js-wpt-repadd', $(this).closest('.js-wpt-field-items')).show();
}
}).data('bound', true);
}
});
};
SetDeleteEvent();
});
This code will restrict my text single line field to 5 repeating items only.
You can adjust this as you need.
Now, you need this only in the first instance of this field, and hence, you could try to apply above JS to a certain div ID only.
In the CRED Form HTML section, you would wrap your first instance only in that div, so the JS applies only on this very DIV and it's nested elements.
https://stackoverflow.com/questions/10279587/make-javascript-only-apply-to-a-div
This is custom code thou, as with the GUI you cannot achieve this.
Simpler would be to use another Field.
I am not a fan of repeating fields in a search, but this is just my opinion.
I prefer single values, as they are less affected by issues later.
Hi Beda,
Given that it may cause problems in future I have opted to keep the fields separate and use JS to conditionally hide/show and set filter values on the archive page based on the aforementioned options.
I may however need to use similar code for another field I expect to have in future to thank for providing a reference.