Skip Navigation

[Gelöst] File upload size limit

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:

I have a form using AJAX without the WordPress Media Library manager and want to limit the upload file size. Despite following the documentation and various threads, my current code does not restrict file uploads to the desired size limit.

Solution:

Ensure the validation code runs only for the specific form and use the $_FILES array to check the file size correctly:

add_filter('cred_form_validate', 'my_validation', 10, 2);
function my_validation($error_fields, $form_data) {
    // Ensure this code runs only for the specific form
    $form_id = 12345; // Replace with your actual form ID
    if ($form_data['id'] != $form_id) {
        return $error_fields;
    }
     
    list($fields, $errors) = $error_fields;
     
    // Validate 'wpcf-beleg-ausgabe' field
    if (isset($_FILES['wpcf-beleg-ausgabe']) && $_FILES['wpcf-beleg-ausgabe']['size'] > 10000) {
        $errors['wpcf-beleg-ausgabe'] = 'FILE SIZE TOO BIG';
    }
     
    // Validate 'wpcf-beleg-einnahme' field
    if (isset($_FILES['wpcf-beleg-einnahme']) && $_FILES['wpcf-beleg-einnahme']['size'] > 10000) {
        $errors['wpcf-beleg-einnahme'] = 'FILE SIZE TOO BIG';
    }
     
    return array($fields, $errors);
}

Relevant Documentation:

https://toolset.com/forums/topic/restricting-file-size-and-types-on-file-upload-cred-form/#post-1411409

https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created vor 4 Monaten, 1 Woche. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Dieses Thema enthält 4 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Christopher Amirian vor 4 Monaten.

Assistiert von: Christopher Amirian.

Author
Artikel
#2708748
Screenshot_1.png

Hi support,

I have a form configured as using AJAX without the WordPress Media Library manager (see screenshot), so just simple browser upload dialog. Now I want to limit the upload file size. I tried to follow the documentation (https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate) as well as several different threads, but I just can't get it to work.

This is my code (stripped to the absolute essentials which should prevent the upload of any file above like 10kb in my 2 "File" fiels in any form):

add_filter('cred_form_validate', 'my_validation', 10, 2);
function my_validation($error_fields, $form_data) {
list($fields,$errors)=$error_fields;
if ( $fields['wpcf-beleg-ausgabe']['field_data']['size'] > 10000 ) {
$errors['beleg-ausgabe']='FILE SIZE TOO BIG';
}
if ( $fields['wpcf-beleg-einnahme']['field_data']['size'] > 10000 ) {
$errors['beleg-einnahme']='FILE SIZE TOO BIG';
}
return array($fields,$errors);
}

But I never get any error and I can upload files of any size without restriction.

I also tried it with the "cred_form_ajax_upload_validate" filter, but that also didn't work (which was explained here: https://toolset.com/forums/topic/restricting-file-size-and-types-on-file-upload-cred-form/#post-1410679 ).

I have the form message field in my form:
[cred_field field='form_messages' value='' class='alert alert-warning']

So what am I doing wrong?

Thanks and best regards,
Michael

#2708765

Christopher Amirian
Supporter

Sprachen: Englisch (English )

Hi Michael,

Your approach to using cred_form_validate seems to be correct. Would you please check the ticket below as a reference?

https://toolset.com/forums/topic/restricting-file-size-and-types-on-file-upload-cred-form/#post-1411409

So basically it should be something like this:

add_filter('cred_form_validate', 'my_validation', 10, 2);
function my_validation($error_fields, $form_data) {
    // Ensure this code runs only for the specific form
    $form_id = 12345; // Replace with your actual form ID
    if ($form_data['id'] != $form_id) {
        return $error_fields;
    }
    
    list($fields, $errors) = $error_fields;
    
    // Validate 'wpcf-beleg-ausgabe' field
    if (isset($_FILES['wpcf-beleg-ausgabe']) && $_FILES['wpcf-beleg-ausgabe']['size'] > 10000) {
        $errors['wpcf-beleg-ausgabe'] = 'FILE SIZE TOO BIG';
    }
    
    // Validate 'wpcf-beleg-einnahme' field
    if (isset($_FILES['wpcf-beleg-einnahme']) && $_FILES['wpcf-beleg-einnahme']['size'] > 10000) {
        $errors['wpcf-beleg-einnahme'] = 'FILE SIZE TOO BIG';
    }
    
    return array($fields, $errors);
}


Form ID Check: Ensures the validation is applied only to the specific form you are targeting.

File Size Check: The $_FILES array is used to access the file size for each field. This ensures you are checking the correct data structure.

I did not test the code above. The code is a suggestion to start your work to find the best solution.

Thanks.

#2708893

Hi Christopher,

Your code worked right away. Now I'm not yet 100% sure what the problem in my code was, but I have a suspicion it was the $fields variable, which you replaced with the $_FILES variable. Now I saw in some threads the $_FILES was used (and I also tried to use it but probably did something wrong), but I focused on the $fields variable, because here it was explained you should use $fields: https://toolset.com/errata/a-note-on-using-the-cred_form_ajax_upload_validate-hook/

But that just as a some info/context for you. And maybe if someone else in the future stumbles upon this thread it helps them 🙂

Thanks again and best regards,
Michael

#2710417
Screenshot_3.png
Screenshot_2.png

Hi Christopher,

First of all: Really strange, but I can't seem to open a new ticket in the forum. When I click on "Report a new issue", I'm simply redirected to my account profile page.

Anyway, my follow-up question: The error message is correctly shown, but the file name is still shown in my form (see screenshot 2). And if you simply go ahead and submit the form again, then the post and the "File" field are actually saved without error message (see screenshot 3). BUT: The file isn't actually uploaded. So the "File" field actually links to a non-existing file.

This is obviously really bad. Is this actually the default behaviour for Toolset forms? How can I fix this? The obvious solution would be to clear the field if an error message occurs. I tried different methods to clear the field in the "my_validation" function you provided:
$fields['wpcf-beleg-einnahme'] = array();
and
$fields['wpcf-beleg-einnahme']['value'] = '';

But it doesn't work. I think the $fields variable doesn't actually return the values to the form. Any idea how I can fix this?

Thanks and best regards,
Michael

Neue Threads, die von Christopher Amirian erstellt wurden und mit diesem verbunden sind, sind unten aufgeführt:

https://toolset.com/de/forums/topic/split-form-submits-after-the-error-message/

#2710625

Christopher Amirian
Supporter

Sprachen: Englisch (English )

Hi Michael,

That is strange, I create a new ticket for you and will do my best to answer there.

Thanks.

#2710760