Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ------------------------------------------------------------------------------
- *
- * # Form validation
- *
- * Demo JS code for form_validation.html page
- *
- * ---------------------------------------------------------------------------- */
- // Setup module
- // ------------------------------
- var FormValidation = function() {
- var _componentMultiselect = function() {
- if (!$().multiselect) {
- console.warn('Warning - bootstrap-multiselect.js is not loaded.');
- return;
- }
- // Basic initialization
- $('.multiselect').multiselect();
- // Enable filtering
- $('.multiselect-filtering').multiselect({
- enableFiltering: true,
- enableCaseInsensitiveFiltering: true
- });
- }
- //
- // Setup module components
- //
- // Uniform
- var _componentUniform = function() {
- if (!$().uniform) {
- console.warn('Warning - uniform.min.js is not loaded.');
- return;
- }
- // Initialize
- $('.form-input-styled').uniform({
- fileButtonClass: 'action btn bg-blue'
- });
- };
- var _componentValidation = function() {
- let $csrf = $("meta[name=csrf-token]").attr("content");
- if (!$().validate) {
- console.warn('Warning - validate.min.js is not loaded.');
- return;
- }
- // Initialize
- var validator = $('.form-validate-jquery').validate({
- ignore: 'input[type=hidden], .select2-search__field', // ignore hidden fields
- errorClass: 'validation-invalid-label',
- successClass: 'validation-valid-label',
- validClass: 'validation-valid-label',
- highlight: function(element, errorClass) {
- $(element).removeClass(errorClass);
- },
- unhighlight: function(element, errorClass) {
- $(element).removeClass(errorClass);
- },
- // Different components require proper error label placement
- errorPlacement: function(error, element) {
- // Unstyled checkboxes, radios
- if (element.parents().hasClass('form-check')) {
- error.appendTo( element.parents('.form-check').parent() );
- }
- // Input with icons and Select2
- else if (element.parents().hasClass('form-group-feedback, multiselect') || element.hasClass('select2-hidden-accessible')) {
- error.appendTo( element.parent() );
- }
- // Input group, styled file input
- else if (element.parent().is('.uniform-uploader, .uniform-select') || element.parents().hasClass('input-group')) {
- error.appendTo( element.parent().parent() );
- }
- // Other elements
- else {
- error.insertAfter(element);
- }
- },
- rules: {
- name: {
- minlength: 2,
- required: true
- },
- body: {
- required: true
- },
- target_type: {
- required: true
- },
- 'classes[]': {
- required: function () {
- return $('#target_type').val() === 'class';
- }
- },
- 'students[]': {
- required: function () {
- return $('#target_type').val() === 'student';
- }
- },
- published_date: {
- required: true
- },
- expired_date: {
- required: true
- },
- },
- submitHandler: function(form, event) {
- console.log($('#target_type').val())
- event.preventDefault();
- var form_data = $(form)[0];
- var formData = new FormData(form_data);
- $.ajax({
- url: '/admin/announcements/store',
- type: 'POST',
- data: formData,
- success: function (result) {
- if (result.type === 'success') {
- page_modal.modal('hide');
- validator.resetForm();
- swalInit.fire({
- title: 'Sukses!',
- text: result.text,
- type: 'success',
- timer: 3000,
- }).then((reload) => {
- window.location.reload();
- });
- } else {
- swalInit.fire({
- title: 'Oops...',
- text: result.text,
- type: 'error',
- timer: 3000,
- });
- }
- },
- cache: false,
- contentType: false,
- processData: false
- });
- }
- });
- // Reset form
- $('#reset').on('click', function() {
- validator.resetForm();
- });
- };
- //
- // Return objects assigned to module
- //
- return {
- init: function() {
- _componentMultiselect();
- _componentUniform();
- _componentValidation();
- }
- }
- }();
- // Initialize module
- // ------------------------------
- document.addEventListener('DOMContentLoaded', function() {
- FormValidation.init();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement