sammarks

Untitled

Dec 10th, 2011
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * quadmail.validation.js
  3.  * Quadmail Form Validation plugin
  4.  */
  5.  
  6. QM(function() {
  7.   // Loop through each validation form on the page
  8.   QM('form.validation').each(function() {
  9.     var $element = QM(this), element = this;
  10.     quadmail.debug('validation', 'Found a validation form.');
  11.  
  12.     // Form submit event.
  13.     $element.submit(function() {
  14.       quadmail.debug('validation', 'Validating Form');
  15.  
  16.       // Hide the errors container.
  17.       $element.children('td.validation').hide();
  18.  
  19.       var v = "";
  20.       var values = new Array();
  21.       // Loop through each input to check for validation, and append to the validation data string.
  22.       $element.children('input').each(function() {
  23.         var $input = QM(this);
  24.         var data = $input.attr('data-validation');
  25.         var id = $input.attr('name');
  26.         if (data != null)
  27.           v += name + ":" + data + "|";
  28.  
  29.         // Append the information to the values array.
  30.         values[id] = $input.val();
  31.  
  32.         // Remove the error class.
  33.         $input.removeClass('error');
  34.       });
  35.       values['validation'] = v;
  36.       quadmail.debug('validation', 'Validation string: ' + v);
  37.  
  38.       // Submit the form to the server.
  39.       // TODO: implement loading show here.
  40.       $.post($element.attr('data-action'), values, function(data) {
  41.         // TODO: implement loading hide here.
  42.         if (data.errors != null) {
  43.           // Errors found. Loop through them and add them to the errors list.
  44.           $errorsContainer = $element.children('td.validation');
  45.           if ($errorsContainer != null) {
  46.             $errorsContainer.children('ul').html('');
  47.             for (var i = 0; i < data.errors.length; i++) {
  48.               $errorsContainer.children('ul').append('<li>' + data.errors[i].message + '</li>');
  49.               $element.children('input[name="' + data.errors[i].field + '"]').addClass('error');
  50.             }
  51.             $errorsContainer.show();
  52.           }
  53.         }
  54.         $element.trigger('validated');
  55.       }, 'json');
  56.  
  57.     });
  58.  
  59.   });
  60. });
  61.  
Add Comment
Please, Sign In to add comment