Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.     $('form[name="product"]').on('submit', function () {
  3.         var _self = $(this);
  4.         _self.each(function () {
  5.             var $textarea = $(this);
  6.             $textarea.val(CKEDITOR.instances[$textarea.attr('id')].getData());
  7.         });
  8.         var data = new FormData(_self[0]);
  9.         data.append('ajax', true);
  10.         $.ajax({
  11.                 method: "POST",
  12.                 url: Routing.generate(route, route_params),
  13.                 data: data,
  14.                 cache: false,
  15.                 contentType: false,
  16.                 processData: false,
  17.                 enctype: 'multipart/form-data',
  18.                 beforeSend: function () {
  19.                     $('[data-toggle="tab"]').find('i').remove();
  20.                     $('input,textarea,select').parent().removeClass('has-error').find('span.help-block').remove();
  21.                     _self.find('[type="submit"]').find('i').remove();
  22.                     $('<i class="fa fa-spin fa-spinner"></i>').prependTo(_self.find('[type="submit"]'));
  23.                 }
  24.             })
  25.             .done(function (msg) {
  26.                 if (msg.valid == true) {
  27.  
  28.                 } else if (msg.valid == false) {
  29.                     var errors = JSON.parse(msg.errors);
  30.                     for (var k in errors) {
  31.                         var selector;
  32.                         var path = errors[k].cause.propertyPath.split('.');
  33.                         if (path.length == 2) {
  34.                             selector = _self.attr('name') + '_' + path[1];
  35.                         } else {
  36.                             var path1 = path[0].replace('children[', '');
  37.                             path1 = path1.replace(']', '');
  38.                             var path2 = path[1].replace('data[', '');
  39.                             path2 = path2.replace(']', '');
  40.                             selector = _self.attr('name') + '_' + path1 + '_' + path2 + '_' + path[2];
  41.                         }
  42.                         var helper = '<span class="help-block">' + errors[k].message + '</span>';
  43.                         $('#' + selector).parent().addClass('has-error').append(helper);
  44.                         if ($('a[href="#' + $('#' + selector).closest('.tab-pane').attr('id') + '"]').find('i').length == 0)
  45.                             $('a[href="#' + $('#' + selector).closest('.tab-pane').attr('id') + '"]').prepend('<i class="fa fa-exclamation-triangle color-red"></i>');
  46.                     }
  47.                 }
  48.             })
  49.             .always(function () {
  50.                 _self.find('[type="submit"]').find('i').remove();
  51.             });
  52.         return false;
  53.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement