adding client side accept file type validation hack.
authorDarrel O\'Pry
Fri, 18 Jan 2008 12:24:30 +0000 (12:24 +0000)
committerDarrel O\'Pry
Fri, 18 Jan 2008 12:24:30 +0000 (12:24 +0000)
filefield.js [new file with mode: 0644]
filefield.module

diff --git a/filefield.js b/filefield.js
new file mode 100644 (file)
index 0000000..dbac021
--- /dev/null
@@ -0,0 +1,36 @@
+// $Id$
+
+/**
+ *  Auto Attach standard client side file input validation
+ */
+Drupal.filefieldValidateAutoAttach = function() {
+  $("input[@type='file']").change( function() {
+    $('.filefield-js-error').remove();
+    /**
+     *  add client side validation for the input[@file] accept attribute
+     */
+    if(this.accept.length>1){
+      v = new RegExp('\\.('+(this.accept?this.accept:'')+')$','gi');
+      if (!v.test(this.value)) {
+        var error = 'The file ' + this.value + " is not supported.\n";
+        error += "Only the following file types are supported: \n" + this.accept.replace(/\|/g, ' ');
+        alert(error);
+        // what do I prepend this to? 
+        // .prepend($('<div class="filefield-js-error>"' + error + '</div>'));
+        this.value = ''; 
+        return false;
+      }   
+    }   
+    /**
+     * Add filesize validation where possible
+     */
+
+  }); 
+}
+
+// Global killswitch
+if (Drupal.jsEnabled) {
+  $(document).ready(Drupal.filefieldValidateAutoAttach);
+}
+
index 3744222..21137ed 100644 (file)
@@ -433,6 +433,8 @@ function _filefield_widget_prepare_form_values($node, $field, &$node_field) {
 function _filefield_widget_form($node, $field, &$node_field) {
   drupal_add_js('misc/progress.js');
   drupal_add_js('misc/upload.js');
+  drupal_add_js(drupal_get_path('module', 'filefield') .'/filefield.js');
+
 
   $fieldname = $field['field_name'];
   drupal_add_css(drupal_get_path('module', 'filefield') .'/filefield.css');
@@ -465,6 +467,7 @@ function _filefield_widget_form($node, $field, &$node_field) {
     '#description' => $field['widget']['description'] . t('<br />Allowed extensions: %ext', array('%ext' => $field['widget']['file_extensions'])),
     '#weight' => 9,
     '#tree' => FALSE,
+    '#attributes' => array('accept' => str_replace(' ', '|', trim($field['widget']['file_extensions']))),
   );
 
   $form[$fieldname]['new']['upload'] = array(