Advertisement
ekobudiyanto

Untitled

Jul 1st, 2020
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 5.85 KB | None | 0 0
  1. /* ------------------------------------------------------------------------------
  2.  *
  3.  *  # Form validation
  4.  *
  5.  *  Demo JS code for form_validation.html page
  6.  *
  7.  * ---------------------------------------------------------------------------- */
  8.  
  9.  
  10. // Setup module
  11. // ------------------------------
  12.  
  13. var FormValidation = function() {
  14.  
  15.  
  16.     var _componentMultiselect = function() {
  17.         if (!$().multiselect) {
  18.             console.warn('Warning - bootstrap-multiselect.js is not loaded.');
  19.             return;
  20.         }
  21.  
  22.         // Basic initialization
  23.         $('.multiselect').multiselect();
  24.  
  25.         // Enable filtering
  26.         $('.multiselect-filtering').multiselect({
  27.             enableFiltering: true,
  28.             enableCaseInsensitiveFiltering: true
  29.         });
  30.     }
  31.  
  32.     //
  33.     // Setup module components
  34.     //
  35.  
  36.     // Uniform
  37.     var _componentUniform = function() {
  38.         if (!$().uniform) {
  39.             console.warn('Warning - uniform.min.js is not loaded.');
  40.             return;
  41.         }
  42.  
  43.         // Initialize
  44.         $('.form-input-styled').uniform({
  45.             fileButtonClass: 'action btn bg-blue'
  46.         });
  47.     };
  48.  
  49.     var _componentValidation = function() {
  50.         let $csrf = $("meta[name=csrf-token]").attr("content");
  51.  
  52.         if (!$().validate) {
  53.             console.warn('Warning - validate.min.js is not loaded.');
  54.             return;
  55.         }
  56.  
  57.         // Initialize
  58.         var validator = $('.form-validate-jquery').validate({
  59.             ignore: 'input[type=hidden], .select2-search__field', // ignore hidden fields
  60.             errorClass: 'validation-invalid-label',
  61.             successClass: 'validation-valid-label',
  62.             validClass: 'validation-valid-label',
  63.             highlight: function(element, errorClass) {
  64.                 $(element).removeClass(errorClass);
  65.             },
  66.             unhighlight: function(element, errorClass) {
  67.                 $(element).removeClass(errorClass);
  68.             },
  69.  
  70.             // Different components require proper error label placement
  71.             errorPlacement: function(error, element) {
  72.  
  73.                 // Unstyled checkboxes, radios
  74.                 if (element.parents().hasClass('form-check')) {
  75.                     error.appendTo( element.parents('.form-check').parent() );
  76.                 }
  77.  
  78.                 // Input with icons and Select2
  79.                 else if (element.parents().hasClass('form-group-feedback, multiselect') || element.hasClass('select2-hidden-accessible')) {
  80.                     error.appendTo( element.parent() );
  81.                 }
  82.  
  83.                 // Input group, styled file input
  84.                 else if (element.parent().is('.uniform-uploader, .uniform-select') || element.parents().hasClass('input-group')) {
  85.                     error.appendTo( element.parent().parent() );
  86.                 }
  87.  
  88.                 // Other elements
  89.                 else {
  90.                     error.insertAfter(element);
  91.                 }
  92.             },
  93.             rules: {
  94.                 name: {
  95.                     minlength: 2,
  96.                     required: true
  97.                 },
  98.                 body: {
  99.                     required: true
  100.                 },
  101.                 target_type: {
  102.                     required: true
  103.                 },
  104.                 'classes[]': {
  105.                     required: function () {
  106.                         return $('#target_type').val() === 'class';
  107.                     }
  108.                 },
  109.                 'students[]': {
  110.                     required: function () {
  111.                         return $('#target_type').val() === 'student';
  112.                     }
  113.                 },
  114.                 published_date: {
  115.                     required: true
  116.                 },
  117.                 expired_date: {
  118.                     required: true
  119.                 },
  120.             },
  121.             submitHandler: function(form, event) {
  122.                 console.log($('#target_type').val())
  123.                 event.preventDefault();
  124.                 var form_data = $(form)[0];
  125.                 var formData = new FormData(form_data);
  126.  
  127.                 $.ajax({
  128.                     url: '/admin/announcements/store',
  129.                     type: 'POST',
  130.                     data: formData,
  131.                     success: function (result) {
  132.                         if (result.type === 'success') {
  133.                             page_modal.modal('hide');
  134.                             validator.resetForm();
  135.                             swalInit.fire({
  136.                                 title: 'Sukses!',
  137.                                 text: result.text,
  138.                                 type: 'success',
  139.                                 timer: 3000,
  140.                             }).then((reload) => {
  141.                                 window.location.reload();
  142.                             });
  143.                         } else {
  144.                             swalInit.fire({
  145.                                 title: 'Oops...',
  146.                                 text: result.text,
  147.                                 type: 'error',
  148.                                 timer: 3000,
  149.                             });
  150.                         }
  151.                     },
  152.                     cache: false,
  153.                     contentType: false,
  154.                     processData: false
  155.                 });
  156.  
  157.             }
  158.         });
  159.  
  160.         // Reset form
  161.         $('#reset').on('click', function() {
  162.             validator.resetForm();
  163.         });
  164.     };
  165.  
  166.  
  167.     //
  168.     // Return objects assigned to module
  169.     //
  170.  
  171.     return {
  172.         init: function() {
  173.             _componentMultiselect();
  174.             _componentUniform();
  175.             _componentValidation();
  176.         }
  177.     }
  178. }();
  179.  
  180.  
  181. // Initialize module
  182. // ------------------------------
  183.  
  184. document.addEventListener('DOMContentLoaded', function() {
  185.     FormValidation.init();
  186. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement