You are viewing limited content. For full access, please sign in.

Question

Question

check validation before submit

asked on April 25, 2018

What is the best way to check the entire form for validation before submitting? I am finding that some fields I set to be required with javascript do not show the validation error message after click of an action button (submit, approve, etc).

Thanks!

1 0

Answer

SELECTED ANSWER
replied on April 25, 2018 Show version history

I recommend when you are adding the required status via Javascript - to set both the required attribute and adding a class to help identify them.

$('.myCSSClassName input').attr('required', 'true');
$('.myCSSClassName input').addClass('required');

By the way, that doesn't add the asterisk ( * ) to the field.  But this is a way to do it:

$('.myCSSClassName input').closest('li').find('.cf-label').find('span').find('span').first().append('<span class="cf-required">*</span>');

Take that a step further - assume I want to remove that field requirement upon form Reject, but keep it in place for Approve or Submit (allow a form to be rejected even if it is missing required fields).  I can do this kind of code - it makes the field required, but removes the requirement when you click Reject: 

$(document).ready(function () {

  $('.myCSSClassName input').attr('required', 'true');
  $('.myCSSClassName input').addClass('required');
  $('.myCSSClassName input').closest('li').find('.cf-label').find('span').find('span').first().append('<span class="cf-required">*</span>');
  
  
  //run these steps upon form reject (ignore required fields and also ignore pattern matching).
  $('.action-btn.Reject').on('click', function() {
    //since we added the "required" class name to the field's input, we can
    //run the following code on just the required class - if we wanted this
    //to work on a field that was set to Required in the Layout screen 
    //instead of via the code above, we'd need to reference the class name 
    //and input at this point (i.e. $('.myCSSClassName input').each...) since
    //class names set via the Layout screen are applied to the whole field
    //not just the input part of the field.
    $('.required').each(function() {    
	  $(this).removeAttr('required');
      $(this).removeClass('required'); 
    });
  }); //end of $(".action-btn.Reject").on('click', function() {
	
}); //end of $(document).ready(function () {

(Be sure to disable back-end validation if you are doing this, or you may get an error).

Now, that doesn't exactly answer your question, but I think it is related.  To address your question more specifically, we can look at Submit or Approve buttons by using $('.action-btn.Approve') or $('.action-btn.Submit') instead of $('.action-btn.Reject').  Within that code, if we want to force it to validate before completing the Submit or Approve actions, we can trigger the code: $('#form1').parsley().validate();   Here's a sample: 

//run these steps upon form Approval (if all required fields are complete).
$('.action-btn.Approve').on('click', function() {
  var noErrors = true; 
  $('#form1').parsley().validate();
  $('.parsley-error').each(function() {
    noErrors = false; 
  });
  if (noErrors) { 
    //
    // Code goes here to occur just before form successfully submits (i.e.
    // add notes to a field, 
    // save the username of the submitter to a field, etc.)
    //
  }
  else { 
    //
    // Code goes here to occur if form failed validation.
    // 
  }
}); //end of $(".action-btn.Approve").on('click', function() {

 

3 0
replied on April 25, 2018

Very useful stuff Mattew! Thanks!

$('#form1').parsley().validate();

^this is exactly what I was looking for.

1 0
replied on April 26, 2018

Glad to help!  smiley

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.