Friday, May 14, 2021

Making one of two fields required

Say you have a form that contains 2 fields, a and b, and you want users to provide a value for a or for b; it is ok if they provide values for both a and b, but it isn't ok if they leave both a and b empty. A practical example would be a form where you have 2 fields: "Home phone number" and "Mobile phone number", and you need users to leave at least one of those two numbers.

In Form Builder, with the UI shown below, you can declare a field to be required (the "Yes" radio button), or not required, which is the default (the "No" radio button), but you can also write a formula which determines dynamically whether the field is required (the "Formula" radio button), and this is what allows you to handle a case as the one we're considering here.

Say your two fields are named home-phone and mobile-phone. You want to say that the home phone is required if no mobile phone was provided. You can do this with the formula xxf:is-blank($mobile-phone). Conversely, the formula for the mobile phone will be xxf:is-blank($home-phone).

At runtime, the asterisk to the left of the label also updates as you interact with the form, becoming:

  • A red asterisk if a value is required, and none is provided
  • A black asterisk if a value is required, and one is provided
  • No asterisk if a value is not required, because another phone number was provided

No comments:

Post a Comment