$('#form-mandatory-information').validate({
  onkeyup: function(element) {
    if ($(element).valid()) {
      $(element).closest('.ftr-field').removeClass('error').find('.ftr-field__error').remove();
    }
  },
  submitHandler: function(form){

    var
      $form = $(form),
      $data = $form.serializeObject();

    $.ajax({
      timeout: 20000,
      url: $form.attr('action'),
      type: $form.attr('method'),
      data: $.param($data),
      beforeSend: function () {},
      success: function (json) {
        const { errors, redirect } = json;

        if(errors){
          displayErrors(errors, '#form-mandatory-information');
        }

        if(redirect){
          location.href = redirect;
        }
      },
      error: function (xhr, ajaxOptions, thrownError) {}
    });


  },
  showErrors: function(errorMap, errorList) {
    $.each(errorList, function() {
      $($(this)[0].element).closest('.ftr-field').addClass('error').find('.ftr-field__error').remove();
      $($(this)[0].element).closest('.ftr-field').append('<span class="ftr-field__error">' + $(this)[0].message + '</span>');
    });
  },
  rules: {
    celular: {
      celular: true,
    },
  },
});