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

Question

Question

Forms - Add Rule so that a field is required when shown

asked on August 19, 2014

 This is based on a recommendation from Nathan in  a previous post.

 

Perhaps you can go through each hidden field that is required, and add a rule so that they are required only when they are shown. 

 

I would like to add this rule but my only rule options are Show / Hide.

1 0

Answer

SELECTED ANSWER
replied on August 20, 2014 Show version history

It'll look something like this. Give the field that determines whether your other fields are shown or hidden the toggleField CSS class. Give the fields that, when visible, should be required the req CSS class.
 

$(document).ready(function() {
  $('.toggleField').change(toggleRequiredFields);
  
  function toggleRequiredFields() {
    $('.req input').each(function() {
      if ($(this).is(':hidden')) {
        $(this).removeAttr('required');
      } else {
        $(this).attr('required',true);
      }
    });
  }
});

You may need to adjust the selector at line 5 if you're requiring drop-down, multi-line, or other fields that don't use input boxes.

 

Edit: Updated selector to properly target fields with the req CSS class.

1 0

Replies

replied on August 19, 2014

That is currently not a built in feature. It is a feature request.

0 0
replied on August 20, 2014 Show version history

If you want to do this in Forms 9.1, you'll need to use JavaScript to add the required attribute when the field is visible and remove it when the field is hidden.

0 0
replied on August 20, 2014 Show version history

Without testing much, I believe this should go something like so:

if (.....) {
   $('.ro input').attr('required','false');
} else {
   $('.ro input').attr('required','True');
}

Eric, can you help in providing the javascript that needs to be tested first in order to see if the field has been hidden? I believe that when Forms does it with field rules that certain characteristics will be applied to the field that we can use as a test. 

0 0
replied on August 20, 2014

Haven't tested, but I believe it should be something like this for each field having a unique class instead of 'ro'. You will also need to set this to trigger whenever there is a change that will affect this.

 

if ($('#Field1').hasClass('hidden')) {
   $('.ro input').attr('required','false');
} else {
   $('.ro input').attr('required','True');
}

 

0 0
SELECTED ANSWER
replied on August 20, 2014 Show version history

It'll look something like this. Give the field that determines whether your other fields are shown or hidden the toggleField CSS class. Give the fields that, when visible, should be required the req CSS class.
 

$(document).ready(function() {
  $('.toggleField').change(toggleRequiredFields);
  
  function toggleRequiredFields() {
    $('.req input').each(function() {
      if ($(this).is(':hidden')) {
        $(this).removeAttr('required');
      } else {
        $(this).attr('required',true);
      }
    });
  }
});

You may need to adjust the selector at line 5 if you're requiring drop-down, multi-line, or other fields that don't use input boxes.

 

Edit: Updated selector to properly target fields with the req CSS class.

1 0
replied on August 20, 2014

Interesting implementation Eric. So in this situation you are treating all required fields as if they have the same class assigned to them and iterating through them. This actually reduces the logic/painstaking nuisance of setting up the same code for each field with a slight difference. 

0 0
replied on August 20, 2014

I generally try to avoid anything described as a "painstaking nuisance." smiley

1 0
replied on August 20, 2014

Indeed, whenever something because tedious, i either use Notepad++ to review it and make it less complicated yet more functional, or I just use it to copy-paste-find and replace below until I get what I need. Sometimes thats the only way to do things when they are not able to be done more efficiently.

0 0
replied on August 20, 2014

Thanks Guys! I will test this out.

0 0
replied on June 8, 2015

I am also trying to add "Required Field" to a field once it is not hidden.

As above, I have a check box which governs whether the field is hidden or not.

My hidden field is a drop-down list of users.

Eric, from your statement:

"You may need to adjust the selector at line 5 if you're requiring drop-down, multi-line, or other fields that don't use input boxes."

could you please advise what syntax / variable I should use for a Drop-down box?

 

0 0
replied on June 8, 2015

For drop down fields, use

$('.req select')
2 0
replied on June 9, 2015

Working well, Thanks Alexander

0 0
replied on January 19, 2016

The above code works well for making a specific type of field required - however, what's the best way to toggle not only the required status of the inputs/checkboxes/textareas/etc, but also their labels?  (That is, how should this code be modified so as to alert the user that they will now need to fill out these newly-visible, now-required fields?)

0 0
replied on July 14, 2016

My hidden field is radio button.

Eric, from your statement:

"You may need to adjust the selector at line 5 if you're requiring drop-down, multi-line, or other fields that don't use input boxes."

 

Can anyone tell me what the selector is for a radio button.

0 0
replied on April 3, 2017

I think it is the same for a radio because a radio can only have 1 input selection.

0 0
replied on January 6, 2023

Validate field as required using field rule is supported with new form designer in Forms 11 Update 3.

You can see other changes of Forms 11 Update 3 from  https://support.laserfiche.com/kb/1014413/list-of-changes-for-laserfiche-forms-11-update-3 and get Forms 11 Update 3 from Laserfiche 11 package  https://support.laserfiche.com/kb/1014263/software-versions-and-fixes-included-in-the-laserfiche-11-download-package

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

Sign in to reply to this post.