Skip Navigation

[Resolved] Limit number of repetitions in repeating field

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to limit the number of repetitions in 4 repeating custom fields on a CRED form.

Solution: You must use custom JavaScript to limit these repeating fields on the front-end of the form. The following code works for the 4 fields mentioned here:

jQuery(window).bind("load", function(){
  var maxreps = {
    'wpcf-otros-nombres': 3,
    'wpcf-patrono-a': 3,
    'wpcf-imagenes-para-la-galeria': 6,
    'wpcf-videos-incrustados': 6
  };
      
  jQuery(document).on( "click", ".js-wpt-repadd", function(e){
    var $closest = jQuery(this).closest('.js-wpt-repetitive');
    var isfile = $closest.find('input[type="file"]').length;
    var name = isfile ? $closest.find('.wpt-repetitive-field:first input[type="file"]:first').attr('data-wpt-name') : $closest.find('.wpt-repetitive-field:first input:first').attr('data-wpt-name');
 
 
    // how many repetitions?
    if ( $closest.find('.wpt-repctl').length > maxreps[name] -1 ) {
      // hide button to add more
      jQuery(this).hide();
    }
  });
 
  // add click listener to trash buttons
  jQuery(document).on( "mouseup", ".js-wpt-repdelete", function(e){
    var $closest = jQuery(e.target).closest('.js-wpt-repetitive');
    $closest.find(".js-wpt-repadd").show();
  });
});
This support ticket is created 6 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 10 replies, has 2 voices.

Last updated by Ljuba 6 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#627868

I have some questions (one is general):

1) As I found several different topics about how to limit users in CRED to desired number of usage (6 images for gallery - ie), can somebody explain me what is the current valid way to do it.

2) Same is with question - How to customize 'Add New' title ('Upload another image', 'Add New Name', ...). Should I do all this manually (by scripts) for tens of forms and hundreds of fields???

3) (general) - For me is hard to understand why Toolset don't apply itself in dialogue's most options from API. Example is present issue with 'limiting number of repeating'. Why your developers simply didn't added 'number field' option when 'repeater' is checked (selected), or 'Title' field, as well?

For such approach, Toolset loosing a lot:
- Customers (as simple options are not available - but they are developed under API).
- REPUTATION, as claim that Toolset can be used without coding knowledge is obviously false.
- MUCH MORE RESOURCES, as Support must to answer numberless times on same questions.

Basically, Toolset is so complicated that person need much more than 30 days to realize that nothing is as Toolset advertise. Specifically part from website front page - 'Toolset Helps You Build Advanced WordPress Sites Without Programming'.

Actually, truth is that Toolset user cannot 'go neither in bathroom' (expression) without knowing Java script. I spent already 40 days and I didn't went too far from begin (basically only two substantially different CPT). Certainly, it is far from that how everything is on Toolset, but every 'little one', I stuck with some trivial issue (see two from present ticket).

So, I don't want to say that Toolset 'sucks' (as it is not the truth), but those 'small trivial thinks' certainly makes most of Toolset clients 'on the edge'. You need some Support Team supervisor who will 'collect' all this 'bones' and place it in real place - development priorities, as it looks that somebody from Toolset cannot understand that 'package' makes the product (in most cases) much more before the functionality.

#628128

1) As I found several different topics about how to limit users in CRED to desired number of usage (6 images for gallery - ie), can somebody explain me what is the current valid way to do it.
The only way to limit the number of repeating images is to include some custom JavaScript in your CRED form's JS panel. If only one repeating field is shown in the CRED form, this should work:

( function( $ ) {
    $( window ).bind( 'load', function(){
  
        let maxreps = 6;
  
        $(document).on( "click", ".js-wpt-repadd", function(e){
  
            // how many repetitions?
            if ( $('.wpt-repctl').length > maxreps -1 ) {
                // hide button to add more
                $(this).hide();
            }
        });
  
        // add click listener to trash buttons
        $(document).on( "click", ".js-wpt-repdelete", function(e){
  
            $(".js-wpt-repadd").show();
        });
    });
})( jQuery );

If this does not work for your form, some modifications may be required. Please show me a URL where I can see the form and I will try to make adjustments.

2) Same is with question - How to customize 'Add New' title ('Upload another image', 'Add New Name', ...).
We have a list of available filters you can use to customize these text strings. Please see Beda's post here: https://toolset.com/forums/topic/i-want-to-change-the-delete-button-text-for-a-repeating-image-field/#post-362316

3) (general) - For me is hard to understand why Toolset don't apply itself in dialogue's most options from API. Example is present issue with 'limiting number of repeating'. Why your developers simply didn't added 'number field' option when 'repeater' is checked (selected), or 'Title' field, as well?
I'm not sure I understand the question, sorry! There isn't currently a way to limit the number of repeating images except by using custom JavaScript in the CRED form. If you're asking why this limitation option is not built in to the repeating image field, I don't have a specific reason. If this is a feature you would like to have implemented, I can pass this suggestion along to the developers as a potential enhancement. I can't guarantee they will be implemented. If more people ask for the same feature, it's more likely it will be implemented, so we collect votes for feature requests.

#628396

1) It is more than one field. I build actually only two main CPT (CRED) - Sites and Places. Sites will be ready in day-two and I will upload it from localhost to server.
- let opened ticket
-- give me instruction how to send you link with credentials
2) Clear
3) Forget, I'm just frustrated. However, it is not bad idea to send request.

#628509

Private reply fields are active here.

#628523

I forgot, for now is there only one form (Sites/Sitios) and will be one more (Places/Lugares) - I probably will use conditional logic to avoid more than one CPT (similar to geographical spaces in Sites).

#628855

Which repeating fields should be restricted in this form? How many items should be allowed for each field?

#628871

Yeap, I forgot that you don't speak Spanish.

Last two should be limited to 6 and first two to 3.

#628972

Please check now, I added the following JavaScript code:

jQuery(window).bind("load", function(){
  var maxreps = {
    'wpcf-otros-nombres': 3,
    'wpcf-patrono-a': 3,
    'wpcf-imagenes-para-la-galeria': 6,
    'wpcf-videos-incrustados': 6
  };
     
  jQuery(document).on( "click", ".js-wpt-repadd", function(e){
    var $closest = jQuery(this).closest('.js-wpt-repetitive');
    var isfile = $closest.find('input[type="file"]').length;
    var name = isfile ? $closest.find('.wpt-repetitive-field:first input[type="file"]:first').attr('data-wpt-name') : $closest.find('.wpt-repetitive-field:first input:first').attr('data-wpt-name');


    // how many repetitions?
    if ( $closest.find('.wpt-repctl').length > maxreps[name] -1 ) {
      // hide button to add more
      jQuery(this).hide();
    }
  });

  // add click listener to trash buttons
  jQuery(document).on( "mouseup", ".js-wpt-repdelete", function(e){
    var $closest = jQuery(e.target).closest('.js-wpt-repetitive');
    $closest.find(".js-wpt-repadd").show();
  });
});
#628979

Thanks. I will see.

Additional question, is it now maximum number of images/videos 6 or 7 (as repeater is set on 6)?

#628981

I don't understand. The max is 6.

#628991

First my question is for purpose to understand better.

So, I could be wrong, but I understand that original field mean one/first uploaded image (example is image field). If repeater is enabled and set like now to 6, it mean that 'repeater' can be repeated maximum 6 times, but with initial image it makes 7 images. If 'repeater' can be repeated 5 times, it mean maximum total number of images is 6.

However, I tried and it mean that 6 repeaters are also maximum total number of images of 6.

It works and THANK YOU.