How to validate fields on hidden section?

Certified Senior Developer

Hi,

I have a form with 2 button tabs A and B. Each one displays a section with fields (section A and Section B).

Initially, the section A is visible (section B not visible).

How may I validate the fields of the section B?

My Submit button validates only the visible fields section, so, if fields of section A are correct (and fields of Section B are not), the form is wrongly submitted.

Regards

  Discussion posts and replies are publicly visible

Parents Reply
  • 0
    Certified Senior Developer
    in reply to Mike Schmitt

    Oh ok, I see...

    The only piece I miss, is how to save the result of validations from fields-section to this top level section with this common local var.

    Would you have a little example please ?

    a!localVariables(
    	local!sectionAHasValidationError: null,
    	
    	// top level section
    	a!sectionLayout(
    		validations: local!sectionAHasValidationError
    	),
    	
    	// section with error
    	a!sectionLayout(
    		fieldA,
    		fieldB,
    		validations: {
    		  if(
    			and(isnull(fieldA), isnull(fieldB)),
    			a!validationMessage(
    			  message: "You must enter either a phone number or an email address!",
    			  validateAfter: "SUBMIT"
    			), /* How to save the validations to  local!sectionAHasValidationError ? */
    			{}
    		  )
    		}
    	)
    )

Children
  • Try this out:

    a!localVariables(
      local!fieldA1,
      local!fieldA2,
      local!fieldB1,
      local!fieldB2,
      local!display: "Section A",
      
      local!sectionAHasValidationError: or(
        rule!APN_isEmpty(local!fieldA1),
        rule!APN_isEmpty(local!fieldA2)
      ),
      local!sectionBHasValidationError: or(
        rule!APN_isEmpty(local!fieldB1),
        rule!APN_isEmpty(local!fieldB2)
      ),
      
      local!validations: reject(
        rule!APN_isEmpty,
        {
          if(local!sectionAHasValidationError,"Error in Section A",null),
          if(local!sectionBHasValidationError,"Error in Section B",null)
        }
      ),
    
      {
        /*top level section*/
        a!sectionLayout(
          label: "Validation Info",
          validations: local!validations
        ),
        
        a!buttonArrayLayout(
          align: "START",
          buttons: {
            a!buttonWidget(
              label: "Section A",
              value: "Section A",
              disabled: local!display="Section A",
              saveInto: local!display
            ),
            a!buttonWidget(
              label: "Section B",
              value: "Section B",
              disabled: local!display="Section B",
              saveInto: local!display
            )
          }
        ),
    
        a!sectionLayout(
          label: "Section A",
          showWhen: local!display="Section A",
          contents: {
            a!textField(
              label: "Field A1",
              value: local!fieldA1,
              saveInto: local!fieldA1,
              required: true
            ),
            a!textField(
              label: "Field A2",
              value: local!fieldA2,
              saveInto: local!fieldA2,
              required: true
            )
          }
        ),
        a!sectionLayout(
          label: "Section B",
          showWhen: local!display="Section B",
          contents: {
            a!textField(
              label: "Field B1",
              value: local!fieldB1,
              saveInto: local!fieldB1,
              required: true
            ),
            a!textField(
              label: "Field B2",
              value: local!fieldB2,
              saveInto: local!fieldB2,
              required: true
            )
          }
        )
      }
    )

  • 0
    Certified Senior Developer
    in reply to Chris

    Thank you Chris, it works great but you don't use the validation of SectionA and SectionB...

    I would have love to keep the validation of these 2 sections, as these later are in 2 sub-interfaces.


    Is there not any way to reuse the validations and then transmit it (as an object reference) to the main form interface?

  • Here's an updated example, essentially the validations are stored at the top level a!localVariables and reused in both sections:

    a!localVariables(
      local!fieldA1,
      local!fieldA2,
      local!fieldB1,
      local!fieldB2,
      local!display: "Section A",
      
      local!sectionAValidations: reject(
        rule!APN_isEmpty,
        {
          if(and(rule!APN_isEmpty(local!fieldA1),rule!APN_isEmpty(local!fieldA2)),"Section A: Enter either field A or field B",null)
        }
      ),
      local!sectionBValidations: reject(
        rule!APN_isEmpty,
        {
          if(rule!APN_isEmpty(local!fieldB1),"Section B: Field B1 is required",null),
          if(rule!APN_isEmpty(local!fieldB2),"Section B: Field B2 is required",null)
        }
      ),
      
      local!validations: reject(
        rule!APN_isEmpty,
        {
          local!sectionAValidations,
          local!sectionBValidations
        }
      ),
    
      {
        /*top level section*/
        a!sectionLayout(
          label: "Validation Info",
          validations: local!validations
        ),
        
        a!buttonArrayLayout(
          align: "START",
          buttons: {
            a!buttonWidget(
              label: "Section A",
              value: "Section A",
              disabled: local!display="Section A",
              saveInto: local!display
            ),
            a!buttonWidget(
              label: "Section B",
              value: "Section B",
              disabled: local!display="Section B",
              saveInto: local!display
            )
          }
        ),
    
        a!sectionLayout(
          label: "Section A",
          showWhen: local!display="Section A",
          validations: local!sectionAValidations,
          contents: {
            a!textField(
              label: "Field A1",
              value: local!fieldA1,
              saveInto: local!fieldA1,
              required: rule!APN_isEmpty(local!fieldA2)
            ),
            a!textField(
              label: "Field A2",
              value: local!fieldA2,
              saveInto: local!fieldA2,
              required: rule!APN_isEmpty(local!fieldA1)
            )
          }
        ),
        a!sectionLayout(
          label: "Section B",
          showWhen: local!display="Section B",
          validations: local!sectionBValidations,
          contents: {
            a!textField(
              label: "Field B1",
              value: local!fieldB1,
              saveInto: local!fieldB1,
              required: true
            ),
            a!textField(
              label: "Field B2",
              value: local!fieldB2,
              saveInto: local!fieldB2,
              required: true
            )
          }
        )
      }
    )

  • 0
    Certified Senior Developer
    in reply to Chris

    Thanks a lot Chris, I did the same using RI (because of my sub-interfaces)

    but unfortunaltely, because of my sub-interfaces, testing all these fields at the top-level is not a good solution.

    For 2 fields, it works well, but for 30 fields I have to find another way to do it.

    The top-level section does not have to know all these fields of Sections A and B (and neither to have to test it). The top-level section just has to display an error message or not.