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 upload and validation

  1. andreyu
    Member

    Hi,

    I'm trying to make some text fields mandatory UNLESS the user selects a file in a separate file upload field.

    I've used the gform_validation hook to perform custom validation and I've run into a pretty big problem: the value of the file upload field isn't available in my validation function. The file I selected isn't anywhere in the $_POST array. Here's what happens:

    1. I select a file and click the submit button
    2. The custom validation kicks in. It can't find the value for the file input field so returns errors.
    3. I change nothing in the fields and click the submit button again
    4. Now the field value is ok, the validation checks and I'm allowed to continue

    I tried setting the file upload field as "required", just to see what would happen and everything is ok. That is strange to me, as I thought that gform_validation was triggered AFTER the normal validation. So it's weird that the normal validation has access to the field value, but my function doesn't.

    Any help would be greatly appreciated...thanks!

    Posted 12 years ago on Tuesday November 22, 2011 | Permalink
  2. The problem is that uploaded files are not available in the $_POST variable. You need to use $_FILES instead. However, if there are any validation errors in the form, Gravity Forms will have already uploaded the file and it won't be available in the $_FILES variable anymore. In that case you will need to check the $_POST variable.
    The following code snippet should do the trick for you.

    //Note: Replace 1 in "input_1" with your actual file upload field id.
    $uploaded_files = json_decode(rgpost("gform_uploaded_files"));
    $is_file_uploaded = !empty($_FILES["input_1"]["name"]) || isset($uploaded_files->input_1);
    Posted 12 years ago on Tuesday November 22, 2011 | Permalink
  3. andreyu
    Member

    Thanks,. that did the trick.

    Posted 12 years ago on Wednesday November 23, 2011 | Permalink

This topic has been resolved and has been closed to new replies.