Properly ‘control’ Controls on Form | Bitesize Series
Controls added to forms have a single instance to manage their related attributes. It can be done by calling <form-context>.getAttribute('<attribute-name>')
. This is pretty straightforward for setting requirement levels or getting their value; however, extra steps sometimes need to be taken to properly ‘control’ the controls themselves; e.g. hiding and locking.
A control can be, for example, hidden by calling <form-context>.getControl('<control-name>').setVisible(false)
, where the control name is the same as the attribute name. However, if we add the same attribute multiple times to the form, it creates multiple control instances with different names.
Each extra instance will have a number appended to the end; e.g. ‘firstname’ and ‘firstname1’. To access the BPF control, we use the name ‘header_process_firstname’. This might be beneficial in rare cases, but is usually tedious to handle in most.
Naive Approach
The most straightforward way, as you can imagine, is to iterate over all those controls by calling either <form-context>.getControl('firstname').setVisible(false)
, <form-context>.getControl('firstname1').setVisible(false)
, <form-context>.getControl('header_process_firstname').setVisible(false)
.
Alternatively, write a generic function to getControl
by its proper name, and then iterate over an appended index in a loop. Stop the loop if the returned control is null.
Proper Way
Microsoft provided a variable to store the list of controls on the form for a given attribute.
It can be accessed using <form-context>.getAttribute('<attribute-name>').controls
. From here, simple iterate and control:
<form-context>.getAttribute('<attribute-name>').controls.forEach((c) => c.setVisible(false));
Much cleaner and safer to use.