Sha256: 79b1d0c82662e8cafb262db9f689aa12c99a29f99313089cba939eeefd09335d

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

(function($) {
  var showErrorSelector = 'form[data-show-errors]';
  
  $(showErrorSelector).live('ajax:error', function(event, request, status, error) {
    console.log(request);
    // CLIENT ERROR -- server-side validation failed. -- FIXME -should this be 422 only?
    if (request.status >= 400 && request.status < 500) { 
      // if data is html, we replace this content of the form with the content
      // of the form that we've just received back
      var contentType = request.getResponseHeader('Content-Type');
      if (contentType =~ /html/) {
        // wrap in a div incase there are a bunch of floating elements, pull the form out
        var $new_form = $('<div>' + request.responseText + '</div>').find('#' + $(this).attr('id'));
        $(this).html($new_form.html());
        
      } else if (contentType =~ /json/) {
        // we will be receiving an error object back, we can pass it straight into rails_form.js
        this.rails_form('set_errors', $.parseJSON(request.responseText));
      } else {
        throw "ujs-form/show-errors: Don't know how to handle dataType " + request.dataType;
      }
    }
  });
}(jQuery));

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails-ujs-form-0.1.2 app/assets/javascripts/ujs-form/show-errors.js
rails-ujs-form-0.1.1 app/assets/javascripts/ujs-form/show-errors.js
rails-ujs-form-0.1.0 app/assets/javascripts/ujs-form/show-errors.js