Managed file validation on first upload only In D7,I have new_form with a managed_file field to allow my user to upload documents to public://. I'm trying to prevent my user from uploading a document if the file name they are trying to upload already exists in the system. So far I've been successful using this:

function mymodule_file_validate($file) {
  $filename = $file-&gt;filename;
  $errors = array();
  if (file_exists('public://' . $filename)) { 
    $errors[] = t("File exists already!.");
  }
  return $errors;
}


This displays an error if the user browses for the file and presses upload, even before the file submission, which is exactly what I need.

However, I also have another form modify_form in this module that allows users to modify the document and in that case it's okay to upload the document with the same name even though the file name exists already. BUT... the function above blocks my modify_form too. 

Any suggestions how to make the function apply only to my new_form? Or another way to prevent upload if file exists already only on new_form? Thanks!
I ended up adding a custom validator only to the new_form file upload field: 

'#type' =&gt; 'managed_file',
'#upload_validators' =&gt; array(
      'file_validate_unique' =&gt; array($file),
)


and then added a function:

function file_validate_unique($file) { 
   $errors = array();
   $filename = $file-&gt;filename;
   if (file_exists('public://' . $filename)) { 
         $errors[] = t('The file could not be uploaded because a file by that name already exists in the destination directory.');
   }
   return $errors;
}


now it only validates the new_form and outputs an error right when the user tries to upload the managed_file, before the form is even submitted.