Gavya Mehta
Gavya Mehta

Reputation: 251

How to add condition to add a mandatory input field for the user in slack bot using python?

I am creating a slack bot in python using slack bolt. I have created a form like structure for the user to provide input values but I need to specify some inputs as mandatory. I tried using the "optional":true field but did not get any result I need to give the datepicker input and multi select input as mandatory fields for the user where input is necessary. Below is the block I have created:

blocks = [
    {
        "type": "header",
        "text": {
            "type": "plain_text",
            "text": "Title"
        }
    },
    {
        "type": "header",
        "text": {
            "type": "plain_text",
            "text": "Date Duration"
        }
    },
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "_Required Field_"
        }
    },
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "Start Date"
        }
    },
    {
        "type": "input",
        "element": {
            "type": "datepicker",
            "placeholder": {
                "type": "plain_text",
                "text": "Select Start date"
            },
            "action_id": "start_date_action"
        },
        "label": {
            "type": "plain_text",
            "text": " "
        },
        "hint": {
            "type": "plain_text",
            "text": "Please ensure start date should be greater than current date "
        }
    },
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "End Date"
        }
    },
    {
        "type": "input",
        "element": {
            "type": "datepicker",
            "placeholder": {
                "type": "plain_text",
                "text": "Select End date"
            },
            "action_id": "end_date_action"
        },
        "label": {
            "type": "plain_text",
            "text": " "
        },
        "hint": {
            "type": "plain_text",
            "text": "Please ensure end date should be greater than start date "
        }
    },
    {
        "type": "divider"
    },
    {
        "type": "header",
        "text": {
            "type": "plain_text",
            "text": "Field1"
        }
    },
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "_Required Field_"
        }
    },
    {
        "type": "input",
        "hint": {
            "type": "plain_text",
            "text": "Multiple inputs are accepted"
        },
        "element": {
            "type": "multi_static_select",
            "placeholder": {
                "type": "plain_text",
                "text": "Select field"
            },
            "options": [
                {
                    "text": {
                        "type": "plain_text",
                        "text": "11111"
                    },
                    "value": "value-0"
                },
                {
                    "text": {
                        "type": "plain_text",
                        "text": "22222"
                    },
                    "value": "value-1"
                }
            ],
            "action_id": "1_multi_select-action"
        },
        "label": {
            "type": "plain_text",
            "text": " "
        }
    },
    {
        "type": "divider"
    },
    {
        "type": "divider"
    },
    {
        "type": "header",
        "text": {
            "type": "plain_text",
            "text": "Field3"
        }
    },
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "_Optional Field_"
        }
    },
    {
        "type": "input",
        "hint": {
            "type": "plain_text",
            "text": "Multiple inputs are accepted"
        },
        "element": {
            "type": "multi_static_select",
            "placeholder": {
                "type": "plain_text",
                "text": "Select Categories"
            },
            "options": [
                {
                    "text": {
                        "type": "plain_text",
                        "text": "adventure"
                    },
                    "value": "value-0"
                },
                {
                    "text": {
                        "type": "plain_text",
                        "text": "biking"
                    },
                    "value": "value-2"
                }
            ],
            "action_id": "categories_multi_select-action"
        },
        "label": {
            "type": "plain_text",
            "text": " "
        }
    },
    {
        "type": "divider"
    },
    {
        "type": "header",
        "text": {
            "type": "plain_text",
            "text": "Number"
        }
    },
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "_Optional Field_"
        }
    },
    {
        "type": "input",
        "hint": {
            "type": "plain_text",
            "text": "Numeric values only"
        },
        "element": {
            "type": "plain_text_input",
            "action_id": "plain_text_input-action",
            "placeholder": {
                "type": "plain_text",
                "text": "Enter Numeric Value"
            }
        },
        "label": {
            "type": "plain_text",
            "text": " "
        }
    },
    {
        "type": "divider"
    },
    {
        "type": "actions",
        "elements": [
            {
                "type": "button",
                "text": {
                    "type": "plain_text",
                    "text": "Submit"
                },
                "value": "submit_form",
                "action_id": "submit_action"
            }
        ]
    }
]

And passing the black to post a message like this

app.client.chat_postEphemeral(
    channel=body["channel"]["id"],
    user=body['user']['id'],
    blocks=param_block,
    text="text_msg",
    )

Is there a way to consider text input field with just numerical values? I'm also attaching the screenshot of the blockenter image description here

Upvotes: 1

Views: 1044

Answers (1)

hadar gendelman
hadar gendelman

Reputation: 31

You want to write your own validation to verify the string input can be converted to an integer, then use ack(response_action=errors, errors="<your error message here>") when you update the modal using an if statement for your validation, rather than submitting the modal and creating an ephemeral message for the user - who wants to submit a form just to find out it was invalid?

Upvotes: 0

Related Questions