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.
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)
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.