| title | lang | slug | order |
|---|---|---|---|
Listening for view submissions |
en |
view_submissions |
12 |
If a view payload contains any input blocks, you must listen to view_submission events to receive their values. To listen to view_submission events, you can use the built-in view() method. view() requires a callback_id of type str or re.Pattern.
You can access the value of the input blocks by accessing the state object. state contains a values object that uses the block_id and unique action_id to store the input values.
Read more about view submissions in our API documentation.
# Handle a view_submission event
@app.view("view_b")
def handle_submission(ack, body, client, view):
# Assume there's an input block with `block_1` as the block_id and `input_a`
val = view["state"]["values"]["block_1"]["input_a"]
user = body["user"]["id"]
# Validate the inputs
errors = {}
if val is not None and len(val) <= 5:
errors["block_1"] = "The value must be longer than 5 characters"
if len(errors) > 0:
ack(response_action="errors", errors=errors)
return
# Acknowledge the view_submission event and close the modal
ack()
# Do whatever you want with the input data - here we're saving it to a DB
# then sending the user a verification of their submission
# Message to send user
msg = ""
try:
# Save to DB
msg = f"Your submission of {val} was successful"
except Exception as e:
# Handle error
msg = "There was an error with your submission"
finally:
# Message the user
client.chat_postMessage(channel=user, text=msg)