In a form field, how to dynamically set whether it is required?

Which IIQ version are you inquiring about?

SailPoint IIQ 8.4

Share all details about your problem, including any error messages you may have received.

<Field displayName="Field A" name="fieldA" postBack="true" required="true" type="string">
  <AllowedValuesDefinition>
    // List of Strings of allowed values
  </AllowedValuesDefinition>
</Field>
<Field dependencies="fieldA" displayName="Field B" name="fieldB" dynamic="true" type="string">
  <Script>
    <Source>
      // How do I set fieldB as required if fieldA.equals("someString")?
    </Source>
  </Script>
</Field>

Hi everyone. In a form, I have 2 fields, fieldA and fieldB. For fieldA, there is a list of allowed values. How do I set fieldB as required only if fieldA.equals(“someString”)?

Thanks in advance.

You can put the code in hidden script as this is evaluated during “dynamic”.

<Field dependencies="fieldA" displayName="Field B" name="fieldB" dynamic="true" type="string">
   <Attributes>
     <Map>
       <entry key="hidden">
          <value>
              <Script>
                 <Source>
                   if (fieldA is something) {
                      filed.setRequired(true);
                   }
                   return false;
                 </Source>
              </Scrip>
          </value>
        </entry>
    </Map>
   </Attributes>
<Script>
    <Source>
      // How do I set fieldB as required if fieldA.equals("someString")?
    </Source>
  </Script>
</Field>

remember to always return false at the end so the filed will not be actually hidden.

<Field displayName="Field A" name="fieldA" postBack="true" required="true" type="string">
  <AllowedValuesDefinition>
    <Value>
      <List>
        <String>String01</String>
        <String>String02</String>
        <String>String03</String>
      </List>
    </Value>
  </AllowedValuesDefinition>
</Field>
<Field displayName="Field B" name="fieldB" dynamic="true" type="string">
  <Attributes>
    <Map>
      <entry key="hidden">
        <value>
          <Script>
            <Source>
              if (form.getField("fieldA").equals("String03")) {
                field.setRequired(true);
              }
              return false;
            </Source>
          </Script>
        </value>
      </entry>
    </Map>
  </Attributes>
</Field>

Is this implementation correct? This does not seem to update fieldB as required

“field” attribute should refrences current field in this script

@tim089 Welcome to the community!

You were really close. You should inverse your equals check so that it’s null safe and you were missing getValue() to pull the value from the field:

 <Field displayName="Field A" name="fieldA" postBack="true" required="true" type="string">
  <AllowedValuesDefinition>
    <Value>
      <List>
        <String>String01</String>
        <String>String02</String>
        <String>String03</String>
      </List>
    </Value>
  </AllowedValuesDefinition>
</Field>
<Field displayName="Field B" name="fieldB" dynamic="true" type="string">
  <Attributes>
    <Map>
      <entry key="hidden">
        <value>
          <Script>
            <Source>
              if ("String03".equals(form.getField("fieldA").getValue())) {
                field.setRequired(true);
              }
              return false;
            </Source>
          </Script>
        </value>
      </entry>
    </Map>
  </Attributes>
</Field>

3 Likes

Thank you, this change worked :grin:

P.S. is there any documention I can find online on the methods being called here i.e. form.getField().getValue()? I wasn’t able to find it in the IIQ JavaDocs

form is the local context variable reference to a sailpoint.object.Form object. You should be able to find that in the javadocs.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.