Hi, let's try these troubleshooting steps:
- Temporarily disable any custom JavaScript you have included in the Form's JS panel and be sure no JavaScript errors are triggered when the image is uploaded
- Check to be sure the "form_messages" field is included in the Form editor. This field is required for custom validation.
- Turn on PHP logs and add some debug code so we can trace the flow. If you are not familiar with logs, I can show you how to activate them temporarily. Go in your wp-config.php file and look for
define('WP_DEBUG', false);
Change it to:
define('WP_DEBUG', true);
Then add these lines, just before it says 'stop editing here':
ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
Now add error_log statements in your validation code, like this:
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']))
{
error_log(' this is one of the forms where the image size validation should be applied ');
//check if this field instance img is bigger than $size (bytes)
$instance_size = array_pop($fields['wpcf-' . $field_slug]['field_data']['size']);
error_log(' instance size: ' . $instance_size );
if ( $instance_size > $size )
{
//display error message for this field instance
$errors['wpcf-' . $field_slug] = $error_msg;
}
}
//return result
return array($fields,$errors);
}
Refresh the page and try to upload the image again. This process should automatically create an error_log.txt file in your site's root directory. Please copy and paste its contents into your next reply. Once that is done, you can revert the changes you made to wp-config.php and delete the log file.
Followed the instructions to the T... this is what the error log showed:
[22-Apr-2019 12:28:00 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 127
[22-Apr-2019 12:28:00 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 135
[22-Apr-2019 12:28:00 UTC] validation script is invoked
[22-Apr-2019 12:28:34 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 127
[22-Apr-2019 12:28:34 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 135
[22-Apr-2019 12:28:34 UTC] validation script is invoked
Okay thanks, a few follow up questions:
- Is this a repeating image field, or is only one image allowed in this field?
- Is this a generic image field, or an existing image custom field?
Look for this line in the code:
list($fields,$errors)=$error_fields;
Just after that line, add this code:
error_log( print_r( $error_fields, true) );
Please upload the image again and let me know what you find in the logs.
My form has one non-repeating custom image field and one repeating custom image field. I would like to limit both from accepting over 1mb pictures.
I added the code you provided.
Here is what the logs showed:
[22-Apr-2019 20:26:59 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 127
[22-Apr-2019 20:26:59 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 135
[22-Apr-2019 20:26:59 UTC] validation script is invoked
[22-Apr-2019 20:26:59 UTC] Array
(
[0] => Array
(
[wpcf-foto-perfil] => Array
(
[field_data] => Array
(
[name] => Background_fallback_video.png
[type] => image/png
[tmp_name] => /tmp/phpgbl961
[error] => 0
[size] => 1938331
)
)
)
[1] => Array
(
)
)
[22-Apr-2019 20:27:07 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 127
[22-Apr-2019 20:27:07 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 135
[22-Apr-2019 20:27:07 UTC] validation script is invoked
[22-Apr-2019 20:27:07 UTC] Array
(
[0] => Array
(
[wpcf-fotos-para-anuncio] => Array
(
[field_data] => Array
(
[name] => Array
(
[0] => Agencia_modelos.jpeg
)
[type] => Array
(
[0] => image/jpeg
)
[tmp_name] => Array
(
[0] => /tmp/phpXlJNvm
)
[error] => Array
(
[0] => 0
)
[size] => Array
(
[0] => 2457250
)
)
)
)
[1] => Array
(
)
)
Okay at this point I think it's probably best for me to log in and take a look at how this is set up. Please provide login credentials in the private reply fields here. Let me know where I can find this Form on the front-end of the site.
I'm seeing a problem where the API callback isn't receiving the correct Form ID. That's not normal, and I'd like to figure out why that's happening. Is it okay for me to install the Duplicator plugin and create a clone of your site? I can run additional tests locally without affecting your live site.
Yes, I already have that plugin. Do what you need to do.
Thanks, I'm escalating this to the second tier support team and I'll let you know what I find out.
Okay my 2nd tier team was able to pin down the problem with the Form ID. It turns out there is an extra closing div in the Form contents that is causing problems with the parser. Please remove the last closing div tag before the end of the form contents. Then the validation script should begin working as expected for the repeating image field. Please confirm, then we can move on to validating the single image field.
I removed the last </div> but it's not working. Actually now I can't even upload any picture at all, not even a 17kb size picture.
It's working for me, here are two screenshots. Can you clear your cache and try again?
The message is working if the file is bigger than 1MB, that is working but I'm trying to add one smaller than that and it gives an error: "There was an error uploading your file."
I did clear my cache and used incognito browser chrome, but it still says that.
- Are you testing while logged in or logged out?
- I am attaching an image here that is less than 1Mb, please try it and let me know the results.
- Can you attach the image you are uploading so I can test using the same image?
I show you in this video: hidden link
Are you testing while logged in or logged out?
I have to be logged in as a User or Admin to get to that page.
I am attaching an image here that is less than 1Mb, please try it and let me know the results.
I've shown in the video above using your image that it doesn not work.
Can you attach the image you are uploading so I can test using the same image?
I can't attach the image I tried because it's over 1MB.