Sha256: 86170b2ae3138d317a0fa1cf9cbb782ad7f5b29f8ecc9df1fc44556948907234

Contents?: true

Size: 1.89 KB

Versions: 17

Compression:

Stored size: 1.89 KB

Contents

$(document).on("click", '[data-behaviour="form_submit"]', function (e) {
  e.preventDefault();
  var submit = [];
  var form_class = $(this).data("form-class");
  $(
    "." + form_class + " input.required, ." + form_class + " textarea.required",
  ).each(function () {
    $(this).removeClass("is-invalid");
    let isValueNull = $(this).val().trim().length === 0
    if(isValueNull && $(this).attr('type') === 'file') {
      isValueNull = $(`[data-attachment-name="${$(this).attr('id')}_attachments"]`).not('.hidden').length === 0
    }
    if (isValueNull) {
      $(this).addClass("is-invalid");
      $(this)[0].scrollIntoView(true);
      submit.push(true);
    }
  });

  $("." + form_class + " select.required").each(function () {
    $(this).removeClass("is-invalid");
    let selected_value = $(this).val();
    if (!Array.isArray(selected_value)) {
      selected_value = selected_value.trim();
    }
    if (selected_value.length === 0) {
      $(this).parent().find("select").addClass("is-invalid");
      $(this)[0].scrollIntoView(true);
      submit.push(true);
    }
  });
  $(".nested_input_validation").each(function () {
    var class_name;
    class_name = $(this).data("class-name");
    $(this)
      .parents(":nth(1)")
      .find("." + class_name)
      .addClass("hidden");
    if ($(this).val().trim().length === 0) {
      $(this)
        .parents(":nth(1)")
        .find("." + class_name)
        .removeClass("hidden");
      $(this)[0].scrollIntoView(true);
      submit.push(true);
    }
  });
  if (submit.length === 0) {
    $("." + form_class).submit();
    $('[data-behaviour="form_submit_spinner"]').removeClass("visually-hidden");
    return $('[data-behaviour="form_submit"]').addClass("visually-hidden");
  }
});

$(document).on("click", '[data-behaviour="discard_form"]', function (e) {
  e.preventDefault();
  const form = $(this).closest("form");
  form[0].reset();
  window.history.back();
});

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
cm-admin-3.0.12 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-3.0.11 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-3.0.10 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-3.0.9 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-3.0.8 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-3.0.7 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-3.0.6 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-3.0.5 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-3.0.4 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-3.0.3 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-3.0.2 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-3.0.1 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-3.0.0 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-2.4.6 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-2.4.5 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-2.4.4 app/assets/javascripts/cm_admin/form_validation.js
cm-admin-2.4.3 app/assets/javascripts/cm_admin/form_validation.js