Skip Navigation

[Resolved] Limit number of instances of custom fields that can be added in a form

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

Problem:
A Toolset Form includes repeating fields where the user can add additional instances of the field. Client wants to limit how many instances of the field can be added, in such a way that the same technique (with different limits) can be used on multiple fields.

Solution:
A simple solution is to use a little custom JavaScript which hides the "Add New" button once the relevant number of instances has been added.

To be able to target different fields with different limits, add a class to the div wrapping the form field, something like this, where the class 'limit-to-3' is added:

<div class="form-group limit-to-3">
    <label>Owner</label>
    [cred_field field="owner" force_type="field" class="form-control" output="bootstrap"]
</div>

You can then use custom JS like so:

jQuery(function($) {
    $(".limit-to-3 .js-wpt-repadd").click(function(event) {
        $length = this.parentNode.childNodes.length;
        if ($length >= 5) {
            $(this).hide();
        }
    });
});
This support ticket is created 6 years 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.

Our next available supporter will start replying to tickets in about 1.16 hours from now. Thank you for your understanding.

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 4 replies, has 2 voices.

Last updated by khalidK 5 years, 12 months ago.

Assisted by: Nigel.

Author
Posts
#1157257

I want to limit the number of repeating instances for two different fields.

I'm using the code from the following link
https://toolset.com/forums/topic/limit-mumbr-of-instances-of-multiple-repeating-fields/

jQuery(function( $ ) {
$(".js-wpt-repadd").click(function( event ) {
$length = this.parentNode.childNodes.length;
if($length>=5) {
$(this).hide();
}
});
});

This however is assigning the same limit to all fields.

What I want to do is assign different limits to each field

For example:
field='member-email'
Maximum number of repeating instances = 3

field='procedures-1'
Maximum number of repeating instances = 5

How can I do that?

#1157649

Nigel
Supporter

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

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

Hi Khalid

The selector ".js-wpt-repadd" isn't specific enough, it is targeting all of the add new buttons.

You just need to narrow the scope, and the easiest way to do that is to edit the Form where you include these fields and add a class to the container div, e.g. here I add a "limit-to-3" class

	<div class="form-group limit-to-3">
		<label>Owner</label>
		[cred_field field="owner" force_type="field" class="form-control" output="bootstrap"]
	</div>

You can see in the screenshot the generated markup, and it should be clear that I can target just that add new button by changing the selector in the code example you provided to

$("limit-to-3 .js-wpt-repadd")...

You can do the same for the field you want to limit to 5. (Just duplicate the existing code and change the selector; if you were doing anything more complex it would be worth creating a custom function that handled different numbers.)

#1158498

I tried the above, it didn't work as expected

<div class="jumbotron jumbotron-fluid">
  <div class="container">
    <h3 class="display-4">[wpml-string context='science-submission-form']Procedures[/wpml-string]</h3>
    <p class="lead">
      [wpml-string context='science-submission-form']This section should describe the experimental procedures the student(s) carried out, and all the steps used to manufacture the prototype if applicable. Keep in mind the value of diagrams, flow charts, and illustrations[/wpml-string]
    </p>
    <div class="form-group limit-to-5">
          [toolset_access role="Administrator,Teacher,School Staff,MOE Employee" operator="allow"]
          [cred_field field='procedures-1' force_type='field' class='form-control' output='bootstrap']
          [/toolset_access]
	</div>
  </div>
</div>
jQuery(function( $ ) {
   $("limit-to-5 .js-wpt-repadd").click(function( event ) {
    $length = this.parentNode.childNodes.length;
    if($length>=5) {
     $(this).hide();
  }
});
});
#1158631

Nigel
Supporter

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

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

You are missing the . on .limit-to-5 to signify a class in your jQuery selector (my fault, I missed it in my example):

$(".limit-to-5 .js-wpt-repadd").click(function( event ) {

Do you want to try again?

#1162090

My issue is resolved now. Thank you!