Skip to content

Instantly share code, notes, and snippets.

@robertbrodrecht
Last active April 30, 2021 19:45
Show Gist options
  • Save robertbrodrecht/8c0e031d588828eb432871e1fc692a97 to your computer and use it in GitHub Desktop.
Save robertbrodrecht/8c0e031d588828eb432871e1fc692a97 to your computer and use it in GitHub Desktop.
Validate Gravity Forms Empty File Uploads

iOS has a bug with input[type="file"] where a delay in submitting a form from an HTTPS URL after attaching a file will result in an empty zero byte file being sent to the server. To reproduce the issue, create a form with an input[type="file"] on an HTTPS URL, attach a file, then wait 90 seconds before submitting.

If you develop WordPress sites that use Gravity Forms, the above validation filter present an error message when empty, zero-byte files are uploaded.

<?php
add_filter(
'gform_field_validation',
function( $result, $value, $form, $field ) {
if ( $field->type === 'fileupload' ) {
if ( isset( $_FILES[ 'input_' . $field->id ] ) && $_FILES[ 'input_' . $field->id ]['size'] < 1 ) {
$result["is_valid"] = false;
$result["message"] = "There was a problem uploading your resume. If you are using an iOS device, you must submit this form within 30 seconds of attaching your resume. Please click or tap the red x above, add your resume and resubmit. We apologize for the inconvenience.";
}
}
return $result;
},
10,
4
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment