Dropzone表单验证

时间:2014-01-10 15:04:41

标签: javascript php

我正在使用dropzone.js创建一个drop zone表单。当表单提交为空时,表单将通过提交空ULR而失败。如何重新验证表单以检查字段是否为空?

这是我的代码。感谢您的帮助。

HTML CODE(index.php)

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<head>   
<link href="../CSS/dropzone.css" type="text/css" rel="stylesheet" />
</head>

<body>

     

<form id="uploader" class="dropzone" action="upload.php"><BR>    <BR> 
  <div class="dropzone-previews"></div><BR><BR> <!-- this is were the previews should be shown. -->
 <div id="previews" class="dropzone-previews"></div>
  <!-- Now setup your input fields -->
  <input type="email" name="username" />
  <input type="password" name="password" />
  <input type="hidden" name="additionaldata" value="1" />

  <button type="submit">Submit data and files!</button>
</form>



</body>
</html> 

upload.php的

<?php
$ds          = DIRECTORY_SEPARATOR;  //1

$storeFolder = '../upload_test/uploads';   //2

if (!empty($_FILES)) {


 $tempFile = $_FILES['file']['tmp_name'];          //3             

 $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;  //4

 $targetFile =  $targetPath. $_FILES['file']['name'];  //5


$allowed =  array('gif','png' ,'jpg');
$filename = $_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
echo 'error';


move_uploaded_file($tempFile,$targetFile); //6

}
?>  
NEW JS custom.dropzone.js which seems to break the upload.php function

Dropzone.options.myAwesomeDropzone = { // The camelized version of the ID of the form               element

  // The configuration we've talked about above
  autoProcessQueue: false,
  parallelUploads: 3,
  maxFiles: 3,
  previewsContainer: ".dropzone-previews",

  accept: function(file, done) {
    console.log("uploaded");
    done();
   },

  init: function() {
  this.on("maxfilesexceeded", function(file){
    alert("No more files please!");
  });
},

// The setting up of the dropzone
init: function() {
  var myDropzone = this;

  // First change the button to actually tell Dropzone to process the queue.
  this.element.querySelector("button[type=submit]").addEventListener("click",   function(e) {
    // Make sure that the form isn't actually being sent.
    e.preventDefault();
    e.stopPropagation();
    myDropzone.processQueue();
   });

   // Listen to the sendingmultiple event. In this case, it's the sendingmultiple event instead
   // of the sending event because uploadMultiple is set to true.
   this.on("sendingmultiple", function() {
  // Gets triggered when the form is actually being sent.
  // Hide the success button or the complete form.
   });
   this.on("successmultiple", function(files, response) {
  // Gets triggered when the files have successfully been sent.
  // Redirect user or notify of success.
  });
  this.on("errormultiple", function(files, response) {
  // Gets triggered when there was an error sending the files.
  // Maybe show form again, and notify user of error
  });
 }

}

2 个答案:

答案 0 :(得分:2)

我今天遇到了类似的问题。 在我的情况下,Form是使用Zend Form Builder构建的(进行所有验证,......) 缩短(至少尽可能短):

  1. 我将表单的现有fileuploadfield更改为标准文本输入字段(甚至可能隐藏) - 作为必填字段
  2. 在表单上方添加 dropzone ,创建了所需的PHP操作,除了将文件从php-tmp文件夹移动到我自己的tmp文件夹(特定于项目)之外什么都不做,然后返回新的路径/文件名为json
  3. 在js中我向我的dropzone添加了一个“on success”功能,该功能采用 json结果并将添加到我输入字段的内容(从1开始)< / LI>
  4. 我的实际表单的动作会使这些路径执行其他一些操作(例如生成用户文件夹,......)并将这些文件(通过dropzone上传)移动到这些新文件夹中。
  5. 希望我能帮助至少一点;)

答案 1 :(得分:1)

Dropzone.js是一个异步框架。 如果你使用这种脚本,你必须以这种方式使用它。

您可以使用JQUERY(AJAX库)。 使用Jquery,您可以在checkfield中验证您的值,并在服务器上同时向您发送图片。

希望它可以帮到你