Tweaking Kentico (1): Hidden required fields

   —   

Today we'll learn how to make hidden fields required once they become visible.

If you've ever played with depending fields in Kentico, you may have run into problems when attempting to make a validated text box appear when another field's state changes. 

In such a case you can't mark the text box as a required field since it would ask the visitor to enter some value even if the field is hidden. Another issue is that the validation you specify for a field only gets applied if the field contains data, not for empty fields. 

This hidden field would usually be associated with some other required field. Let's say you have radio buttons that require something to be selected and for cases when none of the listed options fit, there is the last option called "Other". When a visitor selects this option, a text box appears and they need to fill it with some text. Since the text box can be empty the validation has to be done in the radio buttons field:

General conditionradio.Value!="other" || text.Value!=""

In certain cases, you can create a "dummy" field within an alternative form. You would do this in cases where the validation for your hidden field does not show up in the correct place.  This works because there is no database representation of the new fields you create within an alternative form; make this field required and put the validation inside this newly created field.

For example a form with a checkbox that enables a text box would look like this:

  • checkbox
    • Has depending fields: True
  • textbox
    • Visibility condition: checkbox.Value==True
  • (alternative form) validation (checkbox -> just for validation purposes) ->
    • General condition: checkbox.Value!=True || textbox.Value!=""
    • Caption style: display: none;
    • Input style: display: none;
Share this article on   LinkedIn

Jan Hermann

I am a support engineer in Kentico and I take care of any issue you have!

Comments