Skip Navigation

[Resolved] Minimum & Maximum number of adding Images in Cred of specific repeating field

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

Problem:
Minimum & Maximum number of adding Images in Cred of specific repeating image field

Solution:
You should add the said jQuery code to your Toolset form's JS box.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/minimum-maximum-number-of-adding-images-in-cred-of-specific-repeating-field/#post-901669

Relevant Documentation:

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

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by Minesh 6 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#901538

I followed here:
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

Both solution work to limit how many images the user could upload of a repeating field on Cred post submission

what i want is to allow the user to add a minimum and maximum number of a specific repeating image field

Minimum Images = 4
Maximum Images = 6

This is my Field in CRED

	<div class="form-group">
		<label>Car Images</label>
		[cred_field field='images' post='0km' value='' urlparam='' output='bootstrap']
	</div>	

is it possible to do this?

#901669

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - The best way I could suggest is that you should put a notice before the field informing that for this field user should at least add minimum 4 images and maximum 6 images.

Please add following code to your CRED form's JS box that will hide the button and not allow user to add more than 6 image.

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

$('.js-wpt-repadd').click(function( e ) {
        var length = $('input[type="file"]', $(this).closest('.js-wpt-field-items')).length;
         if(length>6) {
            $(this).hide();
            $('.wpt-repctl').last().hide();
          }
        SetDeleteEvent();
    });

});

But for minimum, you should use CRED hook cred_form_validate and check if user uploaded minimum 4 images.

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

#901992

I tried using your code. It doesn't hide the ADD NEW button after 6 times. It hide it after 7 times of clicking on ADD NEW button.

hidden link

If you look at the end of the form there are 3 upload fields but i want just to hide the ADD NEW button for only one of them not all! is that possible?

It would be great if you just give me a tip how to use the hooks and validation in Cred so i can use this on my own in the future.
In my case, i want to make the user upload minimum 4 images for repeated image field. i tried to look at the hooks docs and see the usage examples but i dont really get the idea how this should work and where to add the code and which hook i should use for this! I will be more than happy if you could show me the code i needed and i can analyse it. my field slug name is (car-images)

Thanks

#902108

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I tried using your code. It doesn't hide the ADD NEW button after 6 times. It hide it after 7 times of clicking on ADD NEW button.
==> Well - as you said you have 3 upload fields, for which field you want to apply the JS code? first, second or third?

if you have looked at the doc, it also contains that examples that you can use and adjust your fields accordingly. You need to add this hooks to your current theme's functions.php file:

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //uncomment this if you want to print the field values
    //print_r($fields);
    //validate if specific form
    if ($form_data['id']==12)
    {
        //check my_field value
        if ($fields['wpcf-my_field']['value']!='correct_value')
        {
            //set error message for my_field
            $errors['wpcf-my_field']='Wrong Value';
        }
        //check if featured image exists
        if (empty($fields['_featured_image']['value']))
        {
            //set error message for featured image
            $errors['_featured_image'] = 'Missing featured image';
        }
    }
    //return result
    return array($fields,$errors);
}

If you have issue with that, please let me know, I would be happy to help.