summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--filefield.js36
-rw-r--r--filefield.module3
2 files changed, 39 insertions, 0 deletions
diff --git a/filefield.js b/filefield.js
new file mode 100644
index 0000000..dbac021
--- /dev/null
+++ b/filefield.js
@@ -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);
+}
+
diff --git a/filefield.module b/filefield.module
index 3744222..21137ed 100644
--- a/filefield.module
+++ b/filefield.module
@@ -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(