Skip Navigation

[Resuelto] Array to String Conversion Error When Image Size Validation Fails

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:

The cred_form_validate filter hook throws PHP waring in editing post form.

Solution:

This is fixed in next version of Toolset Forms plugin.

Relevant Documentation:

This support ticket is created hace 3 años, 6 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Etiquetado: ,

Este tema contiene 9 respuestas, tiene 2 mensajes.

Última actualización por julieP hace 3 años, 5 meses.

Asistido por: Luo Yang.

Autor
Mensajes
#2074861

I have a cred validation hook to restrict the size & type of image files uploaded on the frontend. The Media Library checkbox on form is unchecked :-

add_filter('cred_form_validate', 'ts_validate_data_222',10,2);
function ts_validate_data_222($field_data, $form_data){
        
      $form_ids = array( 222 => true );

    list($fields,$errors)=$field_data;
    
    if ( isset( $form_ids[ $form_data['id'] ] ) ) {
        
        if ( !empty( $fields['wpcf-logo']['value'] ) )  {
              
            $file_type_logo=$fields['wpcf-logo']['file_data']['type'];
            $file_size_logo=$fields['wpcf-logo']['file_data']['size'];    
 
            if ( ( $file_type_logo != 'image/jpeg' ) || ( $file_size_logo > 10240 ) ) {
                $errors['logo'] = 'Please check your image is a JPG/JPEG and smaller than 10KB in size';
            }
        }
    }
    return array($fields,$errors);
}

I've not had any issues with uploading files for ages but now, if the image fails the validation, several thing shappen:-

1. a Toolset generated notice "Because validation failed, please upload the files again" appears
2. the word "Array" is displayed instead of the cred field, and the "choose file" button only shows if the user manually clicks on the X
3. my debug log has these entries:-

"Warning: md5() expects parameter 1 to be string, array given in /home/xxxx/public_html/wp-content/plugins/cred-frontend-editor/application/models/field/wptoolset/class.abstract_credfile.php on line 144

Notice: Array to string conversion in /home/xxxx/public_html/wp-includes/formatting.php on line 1098

Notice: Array to string conversion in /home/xxxx/public_html/wp-includes/formatting.php on line 1098

Notice: Array to string conversion in /home/xxxx/public_html/wp-includes/formatting.php on line 1098

Notice: Array to string conversion in /home/xxxx/public_html/wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/toolset-forms/classes/class.eforms.php on line 598

Notice: Array to string conversion in /home/xxxx/public_html/wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/toolset-forms/classes/class.eforms.php on line 598

Notice: Array to string conversion in /home/xxxx/public_html/wp-includes/formatting.php on line 1098"

Several questions arise out of this:-

1. how do I either prevent generation of the Toolset message (I have already covered this requirement elsewhere on the form, your format just doesn't fit with my design nor does it appear where I want it to) OR how do I hide it?

2. What's the issue causing the errors?

3. Is this a known issue?

#2075471

Hello,

Since it is a custom codes problem, please provide a test site with the same problem, also point out the problem page URL and form URL, where I can edit your custom PHP codes, I need to test and debug it in a live website, thanks

#2076711

I wouldn't call this a custom code problem; the CRED form provides the ability to use or not use the Media Library and the validation code was provided by Toolset support staff.

Additionally, if I remove the validation code and change my WordPress site settings to disallow the upload of jpg and jpeg files and then try to upload one, I get a different error:- "/wp-content/plugins/cred-frontend-editor/library/toolset/cred/embedded/classes/Form_Builder_Helper.php on line 1963" along with an atrociously worded form error "Error some required upload field failed".

Furthermore if I remove the image validation code and validate only the other (single line) field on the form to ensure it contains a value (another routine snippet provided by Toolset), the originally reported error appears concerning the image even if the user doesn't try to change it.

Somewhere during the form processing, certain validation failures trigger the removal of images which is clear because after validation of the form, an array of values are expected that don't exist.

I think you need to investigate from your end. Also, given the time difference between our locations, it would be preferable if this could be passed to a support staff member who is closer to Europe.

#2076893

Since you are using option "Use the WordPress Media Library manager for image, video, audio, or file fields", so your user will be able to upload files with WordPress Media Library before he submit the form.

In this case, you need to use anther filter hook "cred_form_ajax_upload_validate" to validate the uploaded files, see our document:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate
This hook provides custom validation for files that are uploaded using AJAX

#2076947

In my original thread I told you the Media Library box is UNCHECKED

#2076965

Sorry the misunderstanding, I have tried the PHP codes you mentioned above in my localhost with a fresh WP installation, but don't see the PHP error message you mentioned above.

So the problem you mentioned above is abnormal, please check these:
1) Make sure you are using the latest version of Toolset plugins, you can download them here:
https://toolset.com/account/downloads/

2) In case it is a compatibility problem, please deactivate all other plugins, and switch to WordPress default theme 2021, deactivate all custom PHP/JS code snippets, and test again

3) If the problem still persists, please provide database dump file(ZIP file) of your website, you can put the package files in your own google drive disk, share the link only, also point out the problem page URL and form URL, I need to test and debug it in my localhost, thanks
https://toolset.com/faq/provide-supporters-copy-site/

And you can hide the form message by these:
Edit your post form, find and remove the message shortcode:
[cred_field field='form_messages' class='alert alert-warning']

#2077625

I'm just doing a bit more troubleshooting of my own. I've discovered that the warnings only appear when the edit version of the form is submitted. Using a clone of the edit form which I then set to create a post, the warnings don't appear.

When you tested, did you use a create post form or an edit post form?

#2079029

Thanks for the details, I can reproduce the same problem in my localhost, and have escalate this issue, will update here if there is any news.

#2081265

Our developers have provided a hotfix for this issue, you can download it here:
enlace oculto

Unzip above file, use the file "base.php" to replace the same file under Toolset Forms plugin folder file:application\models\form\base.php

Please test it and feedback if the problem is fixed, thanks

#2082259

Hi Luo

Thanks for the hot fix which I can confirm is working. Appreciate the quick turnaround by the developers.