Skip Navigation

[Resolved] Display error if sum of all file sizes, repeated field, cred_form_validate

This support ticket is created 6 years, 11 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by Nigel 6 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#598027

I am trying to calculate the total of all uploaded files and display an error if it is more than a specified value.
The problem is that I can't get any values. I tried to follow your documentation for cred_form_validate.

I have this

unction email_save_maxsize_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']==22263 || $form_data['id']==22257)
    {
        error_log('Form id ok');
        error_log('fields: '.print_r($fields));
        $array_size = $fields['wpcf-attachment']['file_data']['size'];
        error_log('array_size: '.print_r($array_size));
        $max_total = 0;
        foreach($array_size as $size) {
            $max_total = $max_total + $size;
            error_log('Size: '.$size);
        }
        if ($max_total > 3000000) {
            $errors['wpcf-attachment'] = 'Dina bilagors sammanlagda storlek överstiger tillåtet värde – 3MB. Dina bilagor är: '.round($max_total/1000000, 2).'MB tillsammans.';
        }
    }
    //return result
    return array($fields,$errors);
}
add_filter('cred_form_validate','email_save_maxsize_validation', 10, 2);

from error_log:
[12-Dec-2017 10:15:00 UTC] Form id ok
[12-Dec-2017 10:15:00 UTC] fields: 1
[12-Dec-2017 10:15:00 UTC] array_size: 1

#598028

I missed to copy/paste the "f" in "function", it is there in real code 🙂

#598071

Forgot to mention that the field allows multiple instances.

#598189

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Tina

I now have 3 tickets from you, just to let you know I am looking at each of them now and will update you soon.

#598201

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Tina

For this issue, I'm not sure how you are expecting this to work.

If you output $fields to the log so you can inspect it you will see the relevant part looks something like this:

[wpcf-attachment] => Array
        (
            [value] => Array
                (
                    [0] => <em><u>hidden link</u></em>
                    [1] => <em><u>hidden link</u></em>
                )

            [name] => wpcf-attachment
            [type] => credfile
            [repetitive] => 1
            [plugin_type] => types
            [validation] => Array
                (
                    [url2] => Array
                        (
                            [active] => 1
                            [message] => Please enter a valid URL address pointing to the file.
                            [suppress_for_cred] => 1
                        )

                )

        )

The file size isn't available there.

At least on my test site the line $array_size = $fields['wpcf-attachment']['file_data']['size']; doesn't return anything meaningful because the array field wpcf-attachment doesn't contain and entry for file_data.

Because the files are pre-loaded via ajax before you submit the form the global $_FILES variable can't help you out.

You will have to use the PHP filesize function (hidden link) to get the sizes of the already uploaded files and check.

Note that these files already uploaded by CRED, if they are too large you will need to take care of deleting them yourself.

It has been discussed with the developers before about automatically deleting files uploaded as part of a failed form submission and that has been rejected, so your validation routine—if it rejects the files as being too large—will also need to delete them.

Keep me posted with how you get on.