cf7-repeatable-fields icon indicating copy to clipboard operation
cf7-repeatable-fields copied to clipboard

Maximum repeated fields

Open cdheumann opened this issue 6 years ago • 3 comments

it would be nice if I could limit the repetitions.

cdheumann avatar Jul 11 '18 13:07 cdheumann

I needed this so I have implemented it as an event handler in Javascript.

You can use this snippet to hide the add button when a set number of repetitions occur:

$('body').on('wpcf7-field-groups/added wpcf7-field-groups/removed', function (event, $group) {
    var $groupCollection = $(event.target).closest('.wpcf7-field-groups');

    var maxCount = 3;
    var currentCount = $groupCollection.find('.wpcf7-field-group ').length;

    if (currentCount === maxCount) {
        switch (event.type) {
            case "wpcf7-field-groups/added":
                $groupCollection.find(".wpcf7-field-group-add").css('display', 'none');
                break;

            case "wpcf7-field-groups/removed":
                $groupCollection.find(".wpcf7-field-group-add").css('display', 'inline-block');
                break;
        }
    }
});

If the user removes an entry then the add buttons are shown again.

If you have multiple repeaters then you could test for a specific group using:

if($groupCollection.attr('id') == "example-group") {
  // EITHER set a different limit
  maxCount = 10;
  // OR just skip the limits
  return;
}

Note: this may need to be tweaked in the future as I think that there is a bug with the removed event; it triggers before the item is actually removed.

rtpHarry avatar Nov 28 '19 19:11 rtpHarry

Hi,

Your solution is no longer working, do you have an other one ??

Many thanks

calyweb avatar Apr 12 '20 09:04 calyweb

I can see there have been a few commits the plugin since I wrote that snippet, however the site I wrote it for is still working:

https://www.legaledge.co.uk/our-services/trade-marks/form/

I can't see anything that would be a breaking change, so you might need to do some more investigation into how you have integrated the snippet?

rtpHarry avatar Apr 12 '20 13:04 rtpHarry