Solved

Dropdown Conditional Required validation depending on item chosen.

  • 11 January 2024
  • 14 replies
  • 220 views

Badge +2

I have a form that has a dropdown menu “application” and when a certain item “cres” is chosen there is a formatting rule that shows or hides that text field depending on one certain item. That part works well, but I need required validation for that same item “cres” and field so that if anything else is chosen besides that one item, it is not required, but if that one item “cres” is chosen, that text field is required.  The following formula works as far as validating for if “cres” is chosen, but it still validates required if something else is chosen and the field is hidden. What am I missing?

Application != 'CRES' && IsNullOrEmpty({Self})

icon

Best answer by MegaJerk 16 January 2024, 18:15

View original

14 replies

Userlevel 5
Badge +13

Try adding an Invalidate rule to configure this functionality. Below is how to configure this. I am assuming you are using the Responsive Designer.

  • Select the Text field in the designer and add a new rule.
    - My text field is called Version.
  • Configure the rule.
    - Rule name = Validate Version
    - When = and(equals(Application, "Cres"), isNullOrEmpty(Version))
    - Then = Invalidate
    - Validation message = Please enter a Version
  • Try this out in the Preview

 

Userlevel 5
Badge +14

Let’s say you have the following form:

 

Here is the Application Type Control configuration:

 

And the CRES Info Control’s configuration:

 

And a Formatting Rule for the CRES Info Control that will enable it with Application_Type has been set to “CRES”:

 

 

And a Validation Rule. Remember that Validation Rules will *RUN* when they evaluate to TRUE. In your case that means you want to check and see if the Cres Info Control is empty when the Application_Type is set to “CRES”. If both of those are true, invalidate the control:

 

Here’s the formula in a more expansive window:

 

When we try to submit the Form now with an Application_Type of “CRES” and an empty CRES_Info Control, it shows us the Validation Message:

 

That should make it Validate exactly when you’d like it to.

 

Here is some BONUS content.

 

Let’s say that now you ALSO want to clear the value of the CRES_Info Control in the event that someone selects an Application_Type that’s different, but *after* they’ve put some info into the CRES_Info control.

 

So, here is the normal form, someone has put info into the CRES_Info control:

 

But the person entering the data realizes that the Application Type shouldn’t be set to CRES. It should be CARO:

 

But it leaves the data in our CRES Info control! No good! It’s easy enough to change the Application Type back to CRES, delete the stuff in the CRES_Info box, and be on your way, but it can also be automated to clear the contents of CRES_Info if the Application_Type doesn’t equal “CRES”.

 

To do this, we create a new Formatting Rule at the top of the CRES_Info control rules that already exist and leave the two Formatting options unchecked at the bottom:

 

Open the Formula Builder and insert the following code (Don’t close the formula builder yet though!):

(function(formControlCall, application_Type) {
var formControlID = formControlCall.split("'")[1] || "";
var targetControl = sourceContext.find("[formcontrolid='" + formControlID + "'].nf-filler-control").find("[formcontrolid][id]");
var currentValue = targetControl.val();

if (application_Type !== "CRES" && currentValue !== "") {
targetControl.val("").trigger("change");
}

return false;
}("{Control:Self}", Application_Type))

 

At the very bottom, you’ll notice that I use the word “Appliction_Type” that’s the name of the Dropdown Choice control that has the Application Selections. But because I want to actually pipe its value into this rule code, I can’t just use plaintext as a reference to it. So I’ll have to erase the text, go to the “Named Controls” tab in my formula builder, and double click on my Control to insert its reference into the code as shown:

 

NOW, when the Application Control changes its value, it will evaluate this rule to see if there is anything in the CRES_Info Control. If there is, and the Application Type doesn’t equal “CRES” then the value is removed automatically:

 

 

Let me know if you need any more help.

Userlevel 5
Badge +14

Oh, totally did not see that bamaeric had already answered this! Whoops! 

Badge +2

I apologize as I should have posted screen shots. I believe I am using classic form.
Here is the 2 controls:

Application has about 10 options and one of them is CRES.
If CRES is chosen, then it shows the ALDCRES field.

But I only have 2 options for rules. Format and Validation.
 

   I put in this formula:
 

 

And it worked correctly for if CRES was chosen and validated required.
But when I put anything else besides CRES, it still validates required when it isn’t supposed too when hidden on anything besides CRES. You can see in the error messages it still wants the field to be required even though it isn’t shown. I need it to not be shown and no requirement.  

 

 

And just in case this is what I am using for showing and hiding the ALDCRES control
 

 

Userlevel 5
Badge +13

I think you have your parentheses in the formula not matching. Try the formula below instead.

and(equals(Application,"CRES"), isNullOrEmpty(ALDCRES))

Badge +2

That didn’t work at all. There was no validation at all whether I chose CRES or not. I appreciate your help as I have only been able to get it to validate in both conditions, when I only need it required when CRES is chosen.

Userlevel 5
Badge +13

Here are screenshots of my validate rule and the results. Maybe these will help.

 

 

Badge +2

That is exactly what I have.
 

Perhaps the way I am hiding the textbox in the rule is incorrect and is having a conflict?
 

It doesn’t validate when CRES isn’t chosen like it is supposed too, but there is no required validation id CRES is chosen. It’s bizarre. There are no errors.
 

Userlevel 5
Badge +13

Below is what I have for my hide rule on the ALDCRES field.

 

Badge +2

Okay, I have tried several scenarios.
As far as the formatting goes, if I put in this formula:

!contains(Application, 'CRES')

It works as intended. Hides the control unless CRES is chosen from the Application dropdown.


 If I put in the the code you have:
 

not(equals(Application, "CRES"))

It doesn’t work.
 

Both control properties are set to the default settings.
 

 

The formula for the ALDCRES textbox for required field if CRES is shown isn’t working. It doesn’t do anything just lets me submit the form without validation. It’s bizarre. All the other fields are default required validation.  

and(equals(Application,"CRES"), isNullOrEmpty(ALDCRES))

 


 I can get it to validate using this and it works:
 

Application != 'CRES' && IsNullOrEmpty({Self})

But it validates as well when CRES isn’t chosen and the field is hidden.

 

Userlevel 5
Badge +14

The reason it’s not working is because the Application Control is not just a dropdown Choice Control, it’s a Lookup Control. Which means that it’s value is not just a string of what it’s showing, but a combination of the ID of the item its referencing plus the value of whatever you have the Column showing (likely “Title” in this case).

 

That’s why testing for equality has been failing for you but something like the *contains()* runtime function works.

 

To understand what to fix, here is a form with a Lookup Control tied to a list of Fruits, and two Calculated Controls, both of which are referencing the value of whatever is selected in the Fruits Lookup, but parsed slightly differently:

 

Calculated Control 1:

 

Calculated Control 2:

 

When I run the form and make a selection, notice how the values shown in the Calculated Controls are different:

 

The only difference in how these values are obtained is that in the second Calculated Control, it’s wrapping the value of our Lookup in a function called “parseLookup()”:

 

You should be able to go back and use any one of our formulas to solve your problem, but instead of something like:

and(equals(Application,"CRES"), isNullOrEmpty(ALDCRES))

 

It will need to be:

and(equals(parseLookup(Application),"CRES"), isNullOrEmpty(ALDCRES))

 

Hope this helps to clear things up.

 

 

Badge +2

I went ahead and used a parsed calculated value and it returns the value chosen, but when using the formatting rule.

not(equals(Application, "CRES"))

it just hides the whole ALDCRES text field the whole time. When CRES is chosen it’s still hidden,even if I changed the “Application” to the new hidden “ParsedCresValue” calculated value field.

 
As far as redoing the validation field with the code that was posted above,

and(equals(ParsedCresValue(Application),"CRES"), isNullOrEmpty(ALDCRES))

It doesn’t validate at all.
 

Userlevel 5
Badge +14

what you’re doing there isn’t going to work because it’s not valid code. To be certain, I wasn’t suggesting that you use a Calculated Value in your own work, I was simply using it to visually show you how the runtime function of “parseLookup” worked when dealing with lookup values.

 

Your code: 

not(equals(Application, "CRES"))

 

Should be:

not(equals(parseLookup(Application), "CRES"))

 

Likewise, your code:

and(equals(ParsedCresValue(Application),"CRES"), isNullOrEmpty(ALDCRES))

 

Should be:

and(equals(parseLookup(Application),"CRES"), isNullOrEmpty(ALDCRES))

 

Please try to ensure that your formulas look similar to what I’ve typed here. Both the word “Application” and “ALDCRES” should be Named References to your Controls in the form, and therefore should be color coded red or given a red outline to represent that.

 

Other than that, the other text and functions should remain identical to what I’ve posted.

 

Test, see how it works, if it doesn’t, let us know. 
 

Badge +2

This worked exactly how I needed to it to work. Apologies as I am a noob to Nintex forms and am still learning. I confused myself by naming the calculated value Parsed in the name. 
Thank you very much for the clarifications. 

Reply