How to skip required fields but not the custom validations of fields with a press of button?

I have a form which has a secondary button which is supposed to skip required fields but not the custom validation of fields after pressing that button. Is there a way to do this?

  Discussion posts and replies are publicly visible

Parents
  • Hello Mohits

    Look at the link shared by Shiva. One of the not well documented but powerful this mentioned in that thread is that you can actually use the validationGroup for multiple buttons just like this :

    validationGroup:"button1 button2"

    take a look at this example It might help you

    load(
      local!button1,
      local!button2,
      local!text1,
      local!text2,
      a!formLayout(
        contents: a!columnsLayout(
          columns: {
            a!columnLayout(
              a!textfield(
                label: "text1",
                required: true,
                value: local!text1,
                saveInto: local!text1,
                validationGroup: "button1"/*<<-- NOTE this*/
                
              )
            ),
            a!columnLayout(
              a!textfield(
                label: "texx2",
                required: true,
                value: local!text2,
                saveInto: local!text2,
                validationGroup: "button1 button2"/*<<-- NOTE this*/
                
              )
            )
          }
        ),
        buttons: a!buttonlayout(
          primaryButtons: {
            a!buttonWidgetSubmit(
              label: "button1",
              value: true,
              saveInto: local!button1,
              /*skipvalidation: true*//*<<-- NOTE this*/
              validationGroup: "button1"/*<<-- NOTE this*/
              
            ),
            a!buttonWidgetSubmit(
              label: "button2",
              value: false,
              saveInto: local!button2,
              validationGroup: "button2"/*<<-- NOTE this*/
              
            )
          }
        )
      )
    )

     

    Jose

  • Hi Jose,

    I could use validationgroups if both required= true() and custom validations are not used for the same fields.

    As for my case all the fields are mandatory in the interface and also almost all have a validation for their length of 255 chars.

    This is my primary requirement:

    1) There is a button to save which is a secondary button for which i want to skip the required = true() validation but not the custom validation of length<=255 chars.

    2) I want all the validation, required=true() and length validation to trigger when the submit primary button is triggered.

    I'm unable to satisfy the 1) requirement using validationgroups because the same field has both the validations

  • 0
    Certified Lead Developer
    in reply to mohitshah

    Hi mohits0001,

    Try the below code. Here the required validation is added to the section layout  on SUBMIT

    a!formLayout(
      label: "Allow Save",
      contents: {
        a!sectionLayout(
          contents: a!columnsLayout(
            columns: {
              a!columnLayout(
                contents: {
                  a!textField(
                    label: "Name *",
                    labelPosition: "ABOVE",
                    value: ri!name,
                    saveInto: ri!name,
                    refreshAfter: "UNFOCUS",
                    validations: {
                      if(
                        len(
                          ri!name
                        ) > 50,
                        "Name cannot have more than 50 Characters",
                        ""
                      )
                    }
                  )
                }
              ),
              a!columnLayout(
                contents: {
                  a!textField(
                    label: "Phone Number *",
                    labelPosition: "ABOVE",
                    value: ri!phoneNumber,
                    saveInto: ri!phoneNumber,
                    refreshAfter: "UNFOCUS",
                    validations: {
                      if(
                        len(
                          ri!phoneNumber
                        ) > 10,
                        "Phone Number cannot have more than 10 Characters",
                        ""
                      )
                    }
                  )
                }
              )
            }
          ),
          validations: {
            a!validationMessage(
              message: "Name is required to submit",
              validateAfter: "SUBMIT",
              showWhen: or(
                isnull(
                  ri!name
                ),
                trim(
                  ri!name
                ) = ""
              )
            ),
            a!validationMessage(
              message: "Phone Number is required to submit",
              validateAfter: "SUBMIT",
              showWhen: or(
                isnull(
                  ri!phoneNumber
                ),
                trim(
                  ri!phoneNumber
                ) = ""
              )
            )
          },
          validationGroup: "SUBMIT"
        )
      },
      buttons: a!buttonLayout(
        primaryButtons: {
          a!buttonWidgetSubmit(
            label: "Submit",
            validationGroup: "SUBMIT",
            style: "PRIMARY"
          ),
          a!buttonWidgetSubmit(
            label: "Save",
            saveInto: {},
            style: "NORMAL"
          )
        }
      )
    )

  • Hello Mohits,

    Let me insist with the validationGroup plus a custom validation that you wanted. I insist on this because when coding interfaces I prefer to keep as much as possible the validations close to the field. unless you have a huge form on which you might need a a!validationMessage.

    Finally consider the use of the refreshAfter: "KEYPRESS" as a way to make aware the user that the input is not correct.

    I think the following code can make the work. In the code you can change the validationGroup based of a condition 

    load(
      local!text1: "",
      local!text2: "",
      a!formLayout(
        contents: a!columnsLayout(
          columns: {
            a!columnLayout(
              a!textfield(
                label: "text1",
                required: true,
                value: local!text1,
                saveInto: local!text1,
                validationGroup: if(
                  len(local!text1) > 5,"submit save","submit"/*<<-- NOTE this*/
                )
                ,
                validations: {
                  if(
                    len(
                      local!text1
                    ) > 5,
                    "cannot be greater than 5",
                    null
                  )
                }
              )
            ),
            a!columnLayout(
              a!textfield(
                label: "texx2",
                required: true,
                value: local!text2,
                saveInto: local!text2,
                validationGroup: if(
                  len(local!text2) > 5,"submit save","submit"/*<<-- NOTE this*/
                )
                ,
                validations: {
                  if(
                    len(
                      local!text2
                    ) > 5,
                    "cannot be greater than 5",
                    null
                  )
                }
              )
            )
          }
        ),
        validations: {},
        buttons: a!buttonlayout(
          primaryButtons: {
            
            a!buttonWidgetSubmit(
              label: "submit",
              value: "submit",
              style:"PRIMARY",
              saveInto: ri!button,
              validationGroup: "submit"/*<<-- NOTE this*/
              
            )
          }, 
          secondaryButtons: a!buttonWidgetSubmit(
              label: "save",
              value: "save",
              style:"SECONDARY",
              saveInto: ri!button,
              validationGroup: "save"/*<<-- NOTE this*/
              
            )
        )
      )
    )

     

    BTW: Take a look at VimalKumars proposal which is a valid solution  It will depend on your use case but in those cases I prefer to have a generic message as a warning and add all the validations that you have on each field Like this. 

     a!validationMessage(
      message: "Please <Action> Phone Number <Something>",/* < -- Note this*/
      validateAfter: "SUBMIT",
      showWhen: or(
        isnull(
          ri!phoneNumber
        ),
        trim(
          ri!phoneNumber
        ) = "",
        len(ri!phoneNumber) > 10 /* < -- Note this*/
      )
    )

     

    Jose 

Reply
  • Hello Mohits,

    Let me insist with the validationGroup plus a custom validation that you wanted. I insist on this because when coding interfaces I prefer to keep as much as possible the validations close to the field. unless you have a huge form on which you might need a a!validationMessage.

    Finally consider the use of the refreshAfter: "KEYPRESS" as a way to make aware the user that the input is not correct.

    I think the following code can make the work. In the code you can change the validationGroup based of a condition 

    load(
      local!text1: "",
      local!text2: "",
      a!formLayout(
        contents: a!columnsLayout(
          columns: {
            a!columnLayout(
              a!textfield(
                label: "text1",
                required: true,
                value: local!text1,
                saveInto: local!text1,
                validationGroup: if(
                  len(local!text1) > 5,"submit save","submit"/*<<-- NOTE this*/
                )
                ,
                validations: {
                  if(
                    len(
                      local!text1
                    ) > 5,
                    "cannot be greater than 5",
                    null
                  )
                }
              )
            ),
            a!columnLayout(
              a!textfield(
                label: "texx2",
                required: true,
                value: local!text2,
                saveInto: local!text2,
                validationGroup: if(
                  len(local!text2) > 5,"submit save","submit"/*<<-- NOTE this*/
                )
                ,
                validations: {
                  if(
                    len(
                      local!text2
                    ) > 5,
                    "cannot be greater than 5",
                    null
                  )
                }
              )
            )
          }
        ),
        validations: {},
        buttons: a!buttonlayout(
          primaryButtons: {
            
            a!buttonWidgetSubmit(
              label: "submit",
              value: "submit",
              style:"PRIMARY",
              saveInto: ri!button,
              validationGroup: "submit"/*<<-- NOTE this*/
              
            )
          }, 
          secondaryButtons: a!buttonWidgetSubmit(
              label: "save",
              value: "save",
              style:"SECONDARY",
              saveInto: ri!button,
              validationGroup: "save"/*<<-- NOTE this*/
              
            )
        )
      )
    )

     

    BTW: Take a look at VimalKumars proposal which is a valid solution  It will depend on your use case but in those cases I prefer to have a generic message as a warning and add all the validations that you have on each field Like this. 

     a!validationMessage(
      message: "Please <Action> Phone Number <Something>",/* < -- Note this*/
      validateAfter: "SUBMIT",
      showWhen: or(
        isnull(
          ri!phoneNumber
        ),
        trim(
          ri!phoneNumber
        ) = "",
        len(ri!phoneNumber) > 10 /* < -- Note this*/
      )
    )

     

    Jose 

Children