如何在 Slack 模态中显示关于非输入块的验证错误
How to display validation errors about non-input blocks in Slack modals
问题
我有一个 Slack modal 和一个包含输入元素的 section
块。当用户在未在此输入中输入值的情况下提交我的模态时,我如何将该错误传达给用户?
我的尝试 #1:响应验证错误
Slack 的文档描述了当我收到 view_submission
事件 (https://api.slack.com/surfaces/modals/using#displaying_errors) 时如何验证 input
块。但是如果我 return 一个包含 static_select
的 section
块的错误,那么 Slack 不会显示错误消息(也不会关闭模态)。
我的尝试 #2:将 optional
字段设置为 false
input
块有一个 optional
字段可用于验证。但是 section
块中的 static_select
没有 optional
字段:https://api.slack.com/reference/block-kit/block-elements#select
我的尝试#3:使用input
块
我不能使用 input
块,因为它们不会触发 block_actions
消息(记录在 https://api.slack.com/surfaces/modals/using#interactions)。我需要在用户回答问题时更新模式。
我的尝试 #4:行之有效的绝望解决方案
我可以用 "response_action": "update" 响应来回复 view_submission
事件。在该响应中,在缺少值的输入上方包含一条类似这样的错误消息:
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Please provide a start time:*"
}
}
我不喜欢这个解决方案,因为我怀疑我能否复制 Slack 为 input
块验证提供的漂亮的错误消息 UX。
详情
这是我传递给 views.open 调用的 view
参数的简化版本:
{
"blocks": [
{
"block_id": "start_times",
"type": "section",
"text": {
"type": "plain_text",
"text": "Choose a start time"
},
"accessory": {
"action_id": "start_times",
"type": "static_select",
"placeholder": {
"type": "plain_text",
"text": "Choose start"
},
"options": [
{
"text": {
"type": "plain_text",
"text": "10:27 pm"
},
"value":
"{\"path\":\"bookings/new\",\"date\":\"2020-02-14 04:27:22 UTC\"}"
},
{
"text": {
"type": "plain_text",
"text": "10:45 pm"
},
"value":
"{\"path\":\"bookings/new\",\"date\":\"2020-02-14 04:45:00 UTC\"}"
}
]
}
}
],
"callback_id": "create booking",
"private_metadata":
"{\"channel_id\":\"C6M2A4690\",\"min_start_time\":\"2020-02-14 04:27:22 UTC\",\"path\":\"bookings/create\",\"room_id\":175128,\"selected_end_time\":null,\"selected_start_time\":null}",
"type": "modal",
"submit": {
"type": "plain_text",
"text": "Book"
},
"title": {
"type": "plain_text",
"text": "Booking room"
}
}
如果用户立即点击提交,这是我对 view_submission
事件的回应:
{
"response_action": "errors",
"errors": {
"start_times": "Please complete this required field"
}
}
收到我的回复后,Slack 关闭微调器,使模式保持打开状态,但不显示错误消息。模态看起来与通过 views.open
.
首次打开时完全相同
在返回响应之前将响应的“content_type
”设置为“application/json
”。
如果您使用 Django 作为后端,那么您可以这样做。
error_object = {
"response_action": "errors",
"errors": {
"start_times": "Please complete this required field"
}
}
return HttpResponse(json.dumps(error_object), content_type="application/json")
===OR===
return JsonResponse(error_object)
如果您正在使用 JsonReponse,请不要忘记导入它:
from django.http import JsonResponse
自从您发布问题后,input
块的功能发生了变化。 Earlier this month,引入了 input
块发射 block_actions
有效负载的能力。
在 Block Kit reference docs for the input
block 中您可以看到一个新的 dispatch_action
参数。这是一个布尔值,当设置为 true 时,会导致 input
块内的交互元素在更改时发出 block_actions
有效载荷。
因此,为了更直接地回答您的问题,您的 #3 解决方案现在应该可以在没有您包含的警告的情况下使用。将 static_select
放在 input
块内,并将 dispatch_action
设置为 true
,如下所示:
{
"type": "input",
"dispatch_action": true,
"element": {
"type": "static_select",
"placeholder": {
"type": "plain_text",
"text": "Select an item",
"emoji": true
},
"options": [
{
"text": {
"type": "plain_text",
"text": "*this is plain_text text*",
"emoji": true
},
"value": "value-0"
},
{
"text": {
"type": "plain_text",
"text": "*this is plain_text text*",
"emoji": true
},
"value": "value-1"
},
{
"text": {
"type": "plain_text",
"text": "*this is plain_text text*",
"emoji": true
},
"value": "value-2"
}
],
"action_id": "static_select-action"
},
"label": {
"type": "plain_text",
"text": "Label",
"emoji": true
}
}
收到 view_submission
负载后,您现在可以使用正确的验证错误进行响应并将其显示给用户。而且您仍然可以根据需要接收 block_actions
有效负载。
问题
我有一个 Slack modal 和一个包含输入元素的 section
块。当用户在未在此输入中输入值的情况下提交我的模态时,我如何将该错误传达给用户?
我的尝试 #1:响应验证错误
Slack 的文档描述了当我收到 view_submission
事件 (https://api.slack.com/surfaces/modals/using#displaying_errors) 时如何验证 input
块。但是如果我 return 一个包含 static_select
的 section
块的错误,那么 Slack 不会显示错误消息(也不会关闭模态)。
我的尝试 #2:将 optional
字段设置为 false
input
块有一个 optional
字段可用于验证。但是 section
块中的 static_select
没有 optional
字段:https://api.slack.com/reference/block-kit/block-elements#select
我的尝试#3:使用input
块
我不能使用 input
块,因为它们不会触发 block_actions
消息(记录在 https://api.slack.com/surfaces/modals/using#interactions)。我需要在用户回答问题时更新模式。
我的尝试 #4:行之有效的绝望解决方案
我可以用 "response_action": "update" 响应来回复 view_submission
事件。在该响应中,在缺少值的输入上方包含一条类似这样的错误消息:
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Please provide a start time:*"
}
}
我不喜欢这个解决方案,因为我怀疑我能否复制 Slack 为 input
块验证提供的漂亮的错误消息 UX。
详情
这是我传递给 views.open 调用的 view
参数的简化版本:
{
"blocks": [
{
"block_id": "start_times",
"type": "section",
"text": {
"type": "plain_text",
"text": "Choose a start time"
},
"accessory": {
"action_id": "start_times",
"type": "static_select",
"placeholder": {
"type": "plain_text",
"text": "Choose start"
},
"options": [
{
"text": {
"type": "plain_text",
"text": "10:27 pm"
},
"value":
"{\"path\":\"bookings/new\",\"date\":\"2020-02-14 04:27:22 UTC\"}"
},
{
"text": {
"type": "plain_text",
"text": "10:45 pm"
},
"value":
"{\"path\":\"bookings/new\",\"date\":\"2020-02-14 04:45:00 UTC\"}"
}
]
}
}
],
"callback_id": "create booking",
"private_metadata":
"{\"channel_id\":\"C6M2A4690\",\"min_start_time\":\"2020-02-14 04:27:22 UTC\",\"path\":\"bookings/create\",\"room_id\":175128,\"selected_end_time\":null,\"selected_start_time\":null}",
"type": "modal",
"submit": {
"type": "plain_text",
"text": "Book"
},
"title": {
"type": "plain_text",
"text": "Booking room"
}
}
如果用户立即点击提交,这是我对 view_submission
事件的回应:
{
"response_action": "errors",
"errors": {
"start_times": "Please complete this required field"
}
}
收到我的回复后,Slack 关闭微调器,使模式保持打开状态,但不显示错误消息。模态看起来与通过 views.open
.
在返回响应之前将响应的“content_type
”设置为“application/json
”。
如果您使用 Django 作为后端,那么您可以这样做。
error_object = {
"response_action": "errors",
"errors": {
"start_times": "Please complete this required field"
}
}
return HttpResponse(json.dumps(error_object), content_type="application/json")
===OR===
return JsonResponse(error_object)
如果您正在使用 JsonReponse,请不要忘记导入它:
from django.http import JsonResponse
自从您发布问题后,input
块的功能发生了变化。 Earlier this month,引入了 input
块发射 block_actions
有效负载的能力。
在 Block Kit reference docs for the input
block 中您可以看到一个新的 dispatch_action
参数。这是一个布尔值,当设置为 true 时,会导致 input
块内的交互元素在更改时发出 block_actions
有效载荷。
因此,为了更直接地回答您的问题,您的 #3 解决方案现在应该可以在没有您包含的警告的情况下使用。将 static_select
放在 input
块内,并将 dispatch_action
设置为 true
,如下所示:
{
"type": "input",
"dispatch_action": true,
"element": {
"type": "static_select",
"placeholder": {
"type": "plain_text",
"text": "Select an item",
"emoji": true
},
"options": [
{
"text": {
"type": "plain_text",
"text": "*this is plain_text text*",
"emoji": true
},
"value": "value-0"
},
{
"text": {
"type": "plain_text",
"text": "*this is plain_text text*",
"emoji": true
},
"value": "value-1"
},
{
"text": {
"type": "plain_text",
"text": "*this is plain_text text*",
"emoji": true
},
"value": "value-2"
}
],
"action_id": "static_select-action"
},
"label": {
"type": "plain_text",
"text": "Label",
"emoji": true
}
}
收到 view_submission
负载后,您现在可以使用正确的验证错误进行响应并将其显示给用户。而且您仍然可以根据需要接收 block_actions
有效负载。