Skip Navigation

[Resolved] Limit repetitive fields inputs on CRED forms

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

Problem:
A CRED form includes a repetitive field for images. The client wants to limit how many images the user can upload.

Solution:
This is not possible with Toolset settings and requires writing custom code, specifically jQuery in this case.

A possible solution would be the following, though you may need to modify it for your own use.

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

Add that to the custom JS section of the form, and edit for the maximum number of repetitions.

This support ticket is created 6 years, 10 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 replies, has 3 voices.

Last updated by andyK-6 6 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#607900

Tell us what you are trying to do? I have a upload gallery field i would like to set a restriction on how many images is uploaded is that possible

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#607927

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

Are you talking about a front-end CRED form?

With a repeating image custom field, yes?

#607946

yes

#608463

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

There aren't any options or settings for this in CRED.

To add such functionality you'll need to add some custom code. Note that our support doesn't include providing or supporting custom code, and if needs be you can contact the contractors listed here if you need help: https://toolset.com/contractors/

However, to get you started, you can try the using the following code, which you would add to the custom JS section of your form. You would need to edit the maxreps variable (currently set as 4).

( function( $ ) {
	$( document ).ready( function(){

		let maxreps = 4;

		$(".js-wpt-repadd").on( "click", function(e){

			// how many repetitions?
			if ( $('.wpt-repctl').length >= maxreps -1 ) {
				// hide button to add more
				$(this).hide();
			}
		});

		// add click listener to trash buttons
		$(".click-form").on( "click", ".js-wpt-repdelete," function(e){

			$(".js-wpt-repadd").show();
		});
	});
})( jQuery );
#608478

Thank you my issue has been resolved.

#627112

This code does not work - Please change Line 17 to:

$(".click-form").on( "click", ".js-wpt-repdelete", function(e){