PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

File is uploaded before form is validated

  1. MPC Studios
    Member

    I'm having a problem with a form that has a file upload field. If a file is chosen for upload, it gets completely uploaded before the form is validated, and as it is for a print shop, said files could be quite large, meaning a rather long wait before someone is notified that they've missed a required field.

    I've noticed that if I leave the very first field empty (first name), the validator catches it before the file uploads. But if I fill in a first name, and leave out any of the other required fields, that's when the file gets uploaded before validation.

    How can I fix it so that the validation runs on the entire form before the file gets uploaded?

    This is the form in question: http://www.digiprintshoppe.com/place-order/

    Posted 13 years ago on Thursday April 14, 2011 | Permalink
  2. You need to upgrade to the latest public release. You are running an old development release of Gravity Forms and not the latest version.

    Posted 13 years ago on Thursday April 14, 2011 | Permalink
  3. MPC Studios
    Member

    Ok, I've updated to the latest version, but I'm still having the exact same problem.

    Posted 13 years ago on Friday April 15, 2011 | Permalink
  4. I'm not able to recreate what you described above as far as validation happening before the file upload in certain situations. The file upload always occurs as soon as the form is submitted, and then if there are validation errors the form page will be redisplayed with the validation errors for the user to correct.

    Doing what you described above I filled in all the required fields except the first one (the First Name) field and here is what I saw:

    http://grab.by/9Tkm

    The file still gets uploaded and they don't need to upload it again. It gets uploaded to a temporary location. When the final form page is submitted it then gets moved to the correct file location as part of the final form submission.

    This is how it is designed to work. The file HAS to get uploaded because of how browsers handle file upload fields. If we didn't do it this way, the user would have to manually select the file again, which would be cumbersome if you have a bunch of file upload fields and they miss one required field. This is because of browser security settings surrounding file upload fields. They can't be pre-populated. So this is why the file upload handles immediately, so the user doesn't have to keep selecting the file whenever a validation error occurs.

    Posted 13 years ago on Friday April 15, 2011 | Permalink
  5. MPC Studios
    Member

    The problem I am having occurs when the first field is filled out, and one or more of the others is left empty. What you tried was the other way around (first field empty, others filled in), and doing it that way, I get the exact same result as you did. I apologize if my description of the problem was unclear.

    Thanks for taking the time to look at it, btw. :-)

    Posted 13 years ago on Friday April 15, 2011 | Permalink
  6. MPC Studios
    Member

    Here's a screenshot: http://digiprintshoppe.com/screenshot.png

    In the status bar at the very bottom, you can see that the file has started uploading before any validation errors are shown. It will go all the way to 100% before the validation lets you know that the last name is missing and the email and phone numbers are invalid.

    As a user trying to send a large file, I'd find this very confusing, and wonder what took it so long to tell me there were errors.

    Posted 13 years ago on Friday April 15, 2011 | Permalink
  7. I just what you described, filled out the first field and left the others empty and validation worked as expected. The file still gets uploaded regardless, so they don't need to pick it again. If we didn't do the upload at that point, not only would they have to correct those fields they would also have to re-select the files because we can't pre-populate that field due to browser security.

    See this screenshot: http://grab.by/9TlD

    So it appears to be working as designed.

    If you are worried about file uploads on forms that aren't completed, they are stored in a temporary location and are cleaned up automatically even if a form is abandoned.

    Posted 13 years ago on Friday April 15, 2011 | Permalink
  8. MPC Studios
    Member

    I am not worried about partial uploads. I think we may have overlapped posts, please see the one I posted just before yours, with the screenshot.

    Posted 13 years ago on Friday April 15, 2011 | Permalink
  9. It's functioning as intended. The upload takes place when the page is submitted. If there are validation errors, those are then displayed but only after the file upload has been completed. What takes so long to send errors is because they can't be shown until the form action is done being processed which doesn't happen until the file upload is completed.

    It works like this, as I explained above, because if we validated first and showed validation errors before the upload... we couldn't pre-populate the file upload field and they would have to then select the file again anytime a validation error UNRELATED to that file upload field were to be triggered.

    To prevent the user having to re-select the file to upload anytime a validation error occurs, we go ahead and do the upload and then show the validation messages.

    It also has to work this way to accomodate multi-page forms to support going forward/backward in the pages. The file upload has to take place when the next page (or submit) button is clicked, otherwise if they went forward and then back they'd have to reselect the file because we can't pre-populate the file upload input type because of browser security.

    Posted 13 years ago on Friday April 15, 2011 | Permalink
  10. MPC Studios
    Member

    Understand, I don't care if the file is uploaded during validation; that's fine. The problem I have is that it waits until the upload is completely finished before telling me there's an error in the form, and for some people, that can be several minutes. From what you describe as expected behavior, and from what I've seen when I leave the first field empty, validation errors should show up almost immediately (or at least within a few seconds), not several minutes after the user has hit the submit button.

    Posted 13 years ago on Friday April 15, 2011 | Permalink
  11. There isn't anyway around that. The upload is part of the form submit action. The form isn't fully submitted until the upload is complete. That is how form submit actions work if they have file upload fields.

    The validation is also done when the form is submitted, so it doesn't know there is errors until the form is submitted and by extension the form isn't completely submitted until file uploads on the form have been completed and the form action is complete.

    Validation isn't done client side, it's done server side to prevent field tampering. If it was done purely client side with javascript you could do validation before a form is submitted, but that isn't how it works.

    Posted 13 years ago on Friday April 15, 2011 | Permalink
  12. MPC Studios
    Member

    Strange, though, that it seems to show the error immediately when I leave the first field empty.

    ...and of course, now that I go to try it again so I can take a screenshot to show you, it isn't doing it anymore. May have been a browser issue those few times, I suppose.

    I guess there isn't much that can be done, then. I very much appreciate your patience with me.

    I have found the forum post outlining how to add a pseudo progress bar for file uploads (which I'm thinking will help alleviate possible confusion), and implemented it successfully (though it's commented out right now). Do you know if it's possible to have it disappear when validation errors come up? Or should I ask this in a new thread?

    Posted 13 years ago on Friday April 15, 2011 | Permalink
  13. I see what Carl is saying, here, but you can validate before the upload is performed, i don't know why he being so stubborn on this point. Its ridiculous is say that users should wait until a file is uploaded, which in my case is up to 20-30 minutes, before they are told they had a typo in their email address.

    I'm planning on implementing some seperate jQuery validation to pick up any errors in the form BEFORE the request is sent to the server. It's very simple and should be an easy fix to this prob, I'll do it tomorrow and re-post to tell others how it can be done.

    Posted 12 years ago on Sunday July 29, 2012 | Permalink
  14. I've also worked around that limitation in the past by having the file upload on another form, after the initial information is validated. Submit form 1 with all the relevant information, then, if you are accepting large files, redirect the visitor to the second form which has the upload functionality.

    Posted 12 years ago on Sunday July 29, 2012 | Permalink
  15. OK, I've implemented some simple client-side form validation to address this problem.

    Go to http://validity.thatscaptaintoyou.com/ and download the jquery.validity plugin (you could use others, but this is the one i used).

    Add the jquery.validity files to you themes js folder, or whatever folder you are keeping the javascript files in. Include the validity javascript file and css file in your themes header.php file.

    <link type="text/css" rel="Stylesheet" href="<?php echo get_template_directory_uri() ?>/js/validity/jquery.validity.css" />
    
    <script type="text/javascript" src="<?php echo get_template_directory_uri() ?>/js/validity/jQuery.validity.min.js"></script>

    Make sure the validity javascript file is included after jQuery has been included.

    Add the following code (you'll need to change it based on your needs and setup, but this should give you a good starting point).

    [php]
    if(is_page('submit'))
    { ?>
    	<script type="text/javascript">
    		jQuery(document).ready(function()
    		{
    			jQuery('form#gform_1').validity(function()
    			{
                	jQuery('input#input_1_1_3, input#input_1_1_6')
    		        .require('Please enter your name');                          
    
    				jQuery('input#input_1_2, input#input_1_2_2')
        			.require('Please enter a valid email address')
    				.match('email')
        			.equal('Email addresses do not match.');
    
    				jQuery('input#input_1_3')
    		        .require('Please enter your phone number')
    				.match('number', 'Please enter your phone number');                  
    
            	}); // End validity function
    
    		}); // End document.ready function
    	</script>
    <?php
    }
    ?>

    This code just tests to see whether the user is on the page with the form, then it loads the validity function, pretty simple. Validity has pretty thorough documentation so if you run into probs check it out. Also, this works best when you aren't using ajax to submit the form.

    You can see the form I've used it on at http://www.bimawards.com.au/submit for a demo.

    Hope this helps!

    Posted 12 years ago on Monday July 30, 2012 | Permalink
  16. Thank you for posting that. Looks like a nice solution. I tested the demo and it works as expected.

    Posted 12 years ago on Monday July 30, 2012 | Permalink
  17. No worries Chris, always keen to help out where i can ;)

    I've also combined the clever jQuery upload progress indicator posted by Kevin Flahaut (http://www.gravityhelp.com/forums/topic/add-a-file-upload-progress-bar) with this client side validation, with these two additions file uploads with gravity forms are really pretty user friendly.

    If anyone wants some help implementing these together let me know and i'll post a short description of what i did.

    Posted 12 years ago on Monday July 30, 2012 | Permalink
  18. Please do post your procedure. I'd like to see how you pulled it off, and others would as well, I'm sure.

    Posted 12 years ago on Tuesday July 31, 2012 | Permalink
  19. OK, its pretty simple to add both the validation and file upload indicator using jquery.validity and Kevin's technique i mentioned.

    Firstly, go ahead and go through the process to add the upload indicator using the guide Kevin made (http://www.gravityhelp.com/forums/topic/add-a-file-upload-progress-bar).

    Make sure you have it working as it should. Then go download jquery.validity at http://validity.thatscaptaintoyou.com add the jquery and css files to your page as outlined in my previous post.

    Now, this is where it's a bit different from the previuos example without the uploading indicator. We need to load the start and end methods of validity manually so we can return the result of the validation - we only want to show the upload indicator if the validation found no errors, if it found errors we want nothing to happen until the errors have been fixed by the user.

    Here's the code you need to put in you theme's header.php file, i'll explain it below.

    <?php
    
    // jQuery form validation code
    
    if(is_page('submit'))
    { ?>
    	<script type="text/javascript">
    		jQuery(document).ready(function()
    		{
    			function validateForm()
    			{
    			    // Start validation:
        			jQuery.validity.start();
    
    			   	jQuery('input#input_1_1_3, input#input_1_1_6')
    		        .require('Please enter your name');                          
    
    				jQuery('input#input_1_2, input#input_1_2_2')
        			.require('Please enter a valid email address')
    				.match('email', 'This field must contain a valid email address')
        			.equal('Email addresses do not match.');
    
    				jQuery('input#input_1_3')
    		        .require('Please enter your phone number')
    				.match('number', 'Please enter your phone number');   
    
    				var result = jQuery.validity.end().valid;
    
    				return result;
    
            	} // End validity function
    
    		      jQuery("#gform_wrapper_1").after("<div id='fakeprogress'><h3>Uploading Files... Please Wait</h3></div>");
    			jQuery("#fakeprogress").hide();
    
    			jQuery("#gform_wrapper_1 .gform_footer input, #gform_wrapper_1 .gform_footer a").on('click', function(e)
    			{
    				if(validateForm())
    				{
    					jQuery("#fakeprogress").delay(1000).show('slow');
    
    				}
    
    				else
    				{
    					e.preventDefault();
    				}
    
    			});
    
    		}); // End document.ready function
    	</script>
    
    <?php
    }
    ?>

    OK, so this code just tests to see if we are on the form page if(is_page('submit), then I manually start the validation usingjQuery.validity.start();

    I then define my validation rules, then manually end the validation and get the validation status with jQuery.validity.end().valid; If there are no errors then jQuery.validity.end().valid returns TRUE, if there are errors i believe it returns FALSE.

    Then we use the code from Kevin to create and hide the dom elements for the upload indicator.

    jQuery("#gform_wrapper_1").after("<div id='fakeprogress'><h3>Uploading Files... Please Wait</h3></div>");
    jQuery("#fakeprogress").hide();

    This next part is where the magic happens, when the submit button is click it tests to see if there were any errors on the form, if not it submits the form and displays the upload indicator - if there were errors it does nothing

    jQuery("#gform_wrapper_1 .gform_footer input, #gform_wrapper_1 .gform_footer a").on('click', function(e)
    			{
    				if(validateForm())
    				{
    					jQuery("#fakeprogress").delay(1000).show('slow');
    
    				}
    
    				else
    				{
    					e.preventDefault();
    				}
    
    			});

    This code is customized to my needs, name of the page the form is on will be different for you, as will the validation rules and possibly the element(s) to attach the on click event handler to.

    To find the id's of the form inputs to use in the validation rules use the firebug plugin for firefox, you can just right-click on an element and find it's class and id.

    If you have trouble following or implementing it just let me know and i'll try to help ;)

    Posted 12 years ago on Wednesday August 1, 2012 | Permalink