One thing about Gravity Forms that really bugged me and a couple of my clients, is the lack of proper file upload error handling. If a file upload fails, for whatever reason, the form just continues it’s routine, leaving the file upload behind. This is not what you want if you rely on your upload for post thumbnails for example.
It would be even better if Gravity Forms added the option to limit the file size as a setting. Reading through the support forums learned me that they are working on it, so hopefully this is just a temporary solution.
This pretty straight forward code extends the validation of a form with upload fields in it.
function custom_validation( $validation_result )
{
// Get the form out of the validation results
$form = $validation_result[ 'form' ];
// Loop trough all the uploaded files
foreach( $_FILES as $key => $file )
{
if( $file[ 'error' ] == 1 )
{ // Error code 1 is a failed upload
// Set validation to false, making the form show the warning
$validation_result[ 'is_valid' ] = false;
// Get the id of the upload field that failed
$exploded = explode( '_', $key );
$input_id = $exploded[ 1 ];
// Set the appropriate vars inside our form
$form[ 'fields' ][ $input_id ][ 'failed_validation' ] = true;
$form[ 'fields' ][ $input_id ][ 'validation_message' ] = 'Upload failed. File size?';
}
}
// Put the form back in the validation result and return it
$validation_result[ 'form' ] = $form;
return $validation_result;
}
add_action( 'gform_validation', 'custom_validation' );
Warning: This code is not completely tested and should therefor be considered experimental. Do not use it in a live environment, without proper testing.
How does it check for file upload size?
It comes down to a very simple check, using the $_FILES upload array for errors. Error code 1 is a failed upload, code 0 is a successful upload. I don’t really know what error code 3 is, but it seems that upon no file upload, error code 4 is set. Gravity Forms does it’s job well in handling required fields, so we’re only checking error code 1 in this code.
The way I get the id of the upload field is pretty dirty, but there is no other way I could think of right now. The $_FILES array only carries the name of the upload field, perhaps some other hack could add the id to that?
Any known downsides to this code?
If a field upload field fails, the file name is still stored inside the upload field. The user has to press a ‘delete’ link right next to it to clear it and be able to upload a new file. This is not really a problem, but it would be wonderful to have a way to clear that upload field too, next to showing the error message.
Where do you place this code?
You can place this code inside your themes functions.php, but you can also (and I highly recommend you do) make a plugin out of it.
When I placed the code in my functions.php file and reloaded the page that the form is on I received a server error.
Correct. There was an error at line 7, caused by the display of code on my blog. Change that line to the one currently in the post, and it works as it should.
Hi,
that line that you were refering is still with an error.
can you write here in the comments section the line?
Thanks
It is fixed now. Hope this way of showing code doesn’t screw up my code.
Hey,
I am using this method to do a custom validation on image uploads. My issue is a bit different as what I am trying to accomplish is to force the user to choose an image with dimensions greater than what I have specified. This I am getting to work as it does kick back the correct error. My problem is when the validation has failed it then gets the user to delete the old image and choose a new one, when the form has submitted the new image it is still uploading the old image that was selected. I am at a loss as to why this is happening. I believe that for some reason the image that was selected before has already been stored and ready to be uploaded. If you have any knowledge on this issue and could shine some light, it would be much appreciated.
Thanks!
I think it is best to ask this question over at the Gravity Forms support forums. Feel free to use this code, or link to it to explain what you’re working with.