how to show the different required message for the same field?

Certified Senior Developer

Hi,

We have requirement like need to show different required message(dynamic message) for a particular field based on two different button clicks.

here are the more details like we have one comments field which is required for both the submit buttons with names like "Add More Info" and "Add More Comments" .

when the user clicks on "Add More Info" button without providing the comments then need to show the required message is like "comments required for More Information".

when the user clicks on "Add More Comments" button without providing the data to comments field then need to show the required message like "comments should not be blank"

 

Can some one suggest any way to achieve this...

-Ram

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer
    Hi, if your comments field is using a!paragraphField, you can take advantage of the "requiredMessage" parameter. If you store the value of the button click into a local variable, then you can do something like requireMessage: if(local!button = "Add More Info", message, if(local!button = "Add More Comments", other message)). Just make sure your buttons have validate set to true, otherwise the required messages won't show.
  • 0
    Certified Senior Developer
    in reply to Dan Lluhi
    Thanks for the prompt response. but the issue is validation happened before the button value stored in to the local variable.
  • 0
    Certified Lead Developer
    in reply to ramp

    Hi, required messages should only show after something on the form has triggered a validation (e.g., a button with validate set to true). For example, if you test out the code below you'll see that the required message only shows after you click on the button

     

    {
      a!paragraphField(
        label: "Paragraph",
        value: "",
        required: true,
        requiredMessage: "abc"
      ),
      a!buttonArrayLayout(
        buttons: a!buttonWidget(
          label: "Button",
          validate: true
        )
      )
    }

  • 0
    Certified Senior Developer
    in reply to Dan Lluhi
    Thanks for code. here my requirement is like required messages should be different for the same paragraph when user clicks on another button.
  • Hi Ram,

    You can store value of button action into local variable which is define under with() then simply you can validate like

    if(local!btnAction = "Add More Comments","Show Message",)
    if(local!btnAction = "Add More Info","Show Message",)
  • 0
    Certified Senior Developer
    in reply to Shubham Aware
    can you please provide the working example. because btnAction can be stored only after required validation was successful.
  • 0
    Certified Lead Developer
    in reply to ramp
    Hi, yes my first post from 9:22 explains how to solve the problem for your use case with different messages. My most recent post is just providing an example for you to see how the required message only appears after a component validates your interface, which triggers the required message.
  • 0
    Certified Lead Developer

    This use case technically violates how buttonWidget save events work, because failed validation (i.e. for missing "required" fields) executes before the buttonWidget saveInto does.  Meaning that your custom required message would not have any value by which to judge which version it should show.  The best solution for this would be to restructure your form logic in such a way that your required message doesn't rely on what the value of the button click was.

    Otherwise, the only working solution I know of, is to create a complex workaround utilizing local variables and conditionally setting the "submit" value of the button, and setting manual validations on the input field.  This is tricky though and I'd only recommend it to seasoned SAIL developers and only when really necessary.

  • Hello Ramp,

    I agree with Mike, the logic should not rely on the button that is pressed why don't you just add the default message or a generic.

    Here is an example how the solution can be.  If you have more fields then this is not going to work as is. 

     

    load(
      local!text: "",
      local!tmpButton,
      a!formLayout(
        contents: a!columnsLayout(
          columns: {
            a!columnLayout(
              contents: {
                a!paragraphField(
                  label: "Comment" ,
                  value: local!text,
                  saveInto: local!text,
                  /*refreshAfter: "KEYPRESS",*/
                  required: not(rule!APN_isBlank(local!tmpButton)),/* < -- Note this*/ 
                  validationGroup: "submit save"
                ), 
                a!richTextDisplayField(/* < -- Note this*/ 
                  showWhen: and (
                      not(rule!APN_isBlank(local!tmpButton)), 
                      rule!APN_isBlank(local!text)
                  ),
                  value:a!richTextItem(
                    text:a!richTextItem(
                      text:if(
                        local!tmpButton = "save",
                        "Please add comment on save",
                        "Please add Note on submit"
                      ),
                      style: "NEGATIVE"
                    ),
                    style: "STRONG"
                  )
                )
              }
            )
          }
        ),
        validations: {},
        buttons: a!buttonlayout(
          primaryButtons: {
            a!buttonWidget(
              showWhen: rule!APN_isBlank(local!text),
              saveInto: local!tmpButton,
              label: "submit Widget",
              value: "submit",
              style: "PRIMARY"
            ),
            a!buttonWidget(
              showWhen: rule!APN_isBlank(local!text),
              saveInto: local!tmpButton,
              label: "save Widget",
              value: "save",
              style: "SECONDARY"
            ),
            a!buttonWidgetSubmit(
              showWhen: not(rule!APN_isBlank(local!text)),
              saveInto: ri!button,
              validationGroup: "submit",
              label: "submit Submit",
              value: "submit",
              style: "PRIMARY"
            ),
            a!buttonWidgetSubmit(
              showWhen: not(rule!APN_isBlank(local!text)),
              saveInto: ri!button,
              label: "save Submit",
              value: "save",
              validationGroup: "save",
              style: "SECONDARY"
            )
          }
        )
      )
    )

    Jose

  • 0
    Certified Senior Developer
    in reply to Mike Schmitt
    Hi Mike,
    This is what exactly i am expecting. for the time being we gave the generic msg for required field. will try to implement the way u suggested work around if they required. Thanks for understanding the use case. i hope its generic use case. most of the forms required these kind of msg dynamically.