Kentico Kwik Tips: How to Setup Conditional Visibility for Kentico Class Fields

May 2, 2016

As a Kentico developer if you only want to display controls and their values for Kentico class fields when a condition has been met you can use Kentico’s Field Visibility Conditions and Dependencies. Below we will look a simple example that allows for one field to be displayed based on the value of another.

First, select the field you would like to change the value of, which will act as the condition for displaying the second field. Set the field properties as you normally would.

Kentico custom field example 1

Scroll down to the bottom of the field’s options form to where you see Field Advanced Settings. Check the Has Depending Fields checkbox and then save the field.

Kentico custom field example 2

Now select (or create) the field you would like to conditionally display and ensure its properties are configured correctly. Here you can see I am setting the Form Control value as a Uni selector because this field is an integer value that represents a class that Kentico knows about. You can also see that I had to set the Object type for the control to the name of the object being displayed in the Uni selector dropdown list. This is one of our Custom Module classes.

Kentico custom field example 3

Finally, scroll down to the bottom of the field options page for the conditionally visible field and again look at the Field Advanced Settings. Set the Visibility Condition to be the boolean expression you want to use to determine if the field should be displayed using the previous field’s value. Here I am checking to see if this SKU is a Subscription SKU - this is my visibility condition for my SubscriptionTypeID Uni selector control. Also be sure to check Depends on another field.

Kentico custom field example 4

Now you should be able to load the form for an instance of your class and see the ability to have the dependent field appear and disappear based on the value of the other.

Kentico-conditional-field-(2).gif

Sean G. Wright is a Chief Solutions Architect at WiredViews. He is also a 2020, 2021, and 2022 Kentico Xperience MVP, and a founding partner of Craft Brewing Business, a B2B publication for the craft brewing industry.

He loves to learn and teach web dev & software engineering, collecting vinyl records, mowing his lawn, and drinking craft beer 🍺.

You can follow him on Twitter, GitHub, and his active blogging about Kentico Xperience on Dev.to.