Skip Navigation

[Resolved] Validate a repeatable audio field's size

This support ticket is created 3 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by shaunV 3 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#1731595

I am using a form to upload audio files to a repeatable field created within toolset and I want to validate the size.

I am using the example found below, for the cred_form_ajax_upload_validate api, but I cannot manage to get it to validate on repeatable fields.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate

I also tried to create a new custom repeatable file field (and not audio) so I could manually validate the type as well as the size but cannot manage to make it work.

Any thoughts? Can you point me in the right direction?
Thank you!

[code]is this how it works?[/code]

#1732733

Hello,

The filter hook cred_form_ajax_upload_validate works only you upload files using WP media library, you can edit your post form, enable option " Use the WordPress Media Library manager for image, video, audio, or file fields", then follow the document you mentioned above to setup the custom codes, for example this thread:
https://toolset.com/forums/topic/limit-size-of-uploaded-image-not-working/#post-1088293

#1733659

Thank you for your answer.

Unfortunately, that code is not validating repeatable audio fields nor with repeating image fields nor single upload fields.

My two fields are wpcf-gallery and wpcf-_preview_filesvendors. I made sure I got my fields from the the Post Metadata.

Any thoughts?

#1733877

Thanks for the feedback, the thread I mentioned above is just an example, you need to customize the codes according to your website settings, for example:
1) There is a custom repeating image field "gallery", which is created with Types plugin
2) You can setup your post form, enable option: Use the WordPress Media Library manager for image, video, audio, or file fields
3) Add below codes in your theme file "functions.php":

add_filter('cred_form_ajax_upload_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //validate if specific form
    if ($form_data['id']==123)
    {
        //get the latest image size
		foreach($fields as $k => $v){
			if(strpos($k, 'wpcf-gallery') !== false){
				$size = $v['field_data']['size'];
				if ($size > 1000000) // no more than 1M 
				{
					$errors['wpcf-gallery'] = 'the image size should not be more than 1M bytes';
				}
			}
		};
    }
    return array($fields,$errors);
}

Please replace 123 with your form's ID

#1733929

Thank you, Luo! That worked great for the both fields.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.