Hi,
I am trying to validate the file upload field using the below code.
when I upload with the restricted extension, the error message shows
But, When I click the submit button again, the form is submitted and data saved. What is the issue? Why does it prevent the user at the first click and allow the submission in the second click?
<?php
add_filter('cred_form_validate','cred_filetype_size_validation',10,2);
function cred_filetype_size_validation($field_data, $form_data)
{
// Field data are field values and errors
list($fields,$errors)=$field_data;
//Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
if (( 5245 == $form_data['id']) && (isset($_FILES['wpcf-card']['type'])) && (isset($_FILES['wpcf-card']['size']))) {
//Retrieve file type
$file_type_uploaded=$_FILES['wpcf-card']['type'];
//Retrieve file size
$file_size_uploaded=$_FILES['wpcf-card']['size'];
//Validate files uploaded, make sure its PDF file type AND not more than 5000kB
if (!( ('application/pdf' == $file_type_uploaded) && ($file_size_uploaded < 5000000) )) {
$errors['card']='Sorry the file you have uploaded is not of the correct type or exceeded 100kB limit.';
}
}
//return result
return array($fields,$errors);
}