Problem: I would like to use the Forms API to validate each image that is uploaded in a single or repeating custom field. It doesn't seem to be working, in the logs I see a PHP notice about trying to get property of a non-object.
Solution: Make sure the User has permission to publish Media if you are using Toolset Access Control. Make sure the Form's markup structure doesn't include any extra closing div tags. Use the following custom code to validate a repeating image field:
add_filter('cred_form_ajax_upload_validate','rep_img_size_validation',10,2); function rep_img_size_validation($error_fields, $form_data) { error_log(' validation script is invoked '); $forms = array( 29, 49 ); $size = 1024000; $field_slug = 'your-repeating-image-slug'; $error_msg = 'Maximum size is 1MB, please try again.'; //field data are field values and errors list($fields,$errors)=$error_fields; //validate if specific form if (in_array( $form_data['id'], $forms ) && isset($fields['wpcf-' . $field_slug]['field_data']['size'])) { //check if this field instance img is bigger than $size (bytes) $instance_size = array_pop($fields['wpcf-' . $field_slug]['field_data']['size']); if ( $instance_size > $size ) { //display error message for this field instance $errors['wpcf-' . $field_slug] = $error_msg; } } //return result return array($fields,$errors); }
To validate a single field, copy the code above and make the following adjustments:
- Change the function names to prevent a PHP error
- Change the field slug
- Remove array_pop from the instance_size definition like this:
// for repeating images // $instance_size = array_pop($fields['wpcf-' . $field_slug]['field_data']['size']); // for single images $instance_size = $fields['wpcf-' . $field_slug]['field_data']['size'];
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate
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 |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 18 replies, has 2 voices.
Last updated by 5 years, 6 months ago.
Assisted by: Christian Cox.