如何在 Slack 模态中显示有关非输入块的验证错误

Gab*_*eal 8 slack-api slack

我有一个 Slack模态,其中section包含一个包含输入元素的块。当用户提交我的模态而没有在此输入中输入值时,我如何将该错误传达给用户?

我的尝试 #1:响应验证错误

Slack 的文档描述了如何input在我收到view_submission事件时验证块( https://api.slack.com/surfaces/modals/using#displaying_errors )。但是,如果我为section包含 a的块返回错误static_select,则 Slack 不会显示错误消息(也不会关闭模式)。

我的尝试 #2:将optional字段设置为 false

input块有一个optional可用于验证的字段。但是块static_select中的 asection没有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:绝望的解决方案

我可以view_submission"response_action": "update"响应来回复事件。在该响应中,在具有缺失值的输入上方包含这样的错误消息:

{
  "type": "section",
  "text": {
    "type": "mrkdwn",
    "text": "*Please provide a start time:*"
  }
}
Run Code Online (Sandbox Code Playgroud)

我不喜欢这个解决方案,因为我怀疑我是否可以复制 Slack 为input块验证提供的良好的错误消息 UX 。

细节

这是view我传递给views.open调用的参数的简化版本:

{
  "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"
  }
}
Run Code Online (Sandbox Code Playgroud)

如果用户立即点击提交,这是我对view_submission事件的回应:

{
  "response_action": "errors",
  "errors": {
    "start_times": "Please complete this required field"
  }
}
Run Code Online (Sandbox Code Playgroud)

收到我的回复后,Slack 关闭微调器,让模式保持打开状态,但不显示错误消息。模态看起来与第一次通过views.open.

Mat*_*ton 7

自从您发布问题以来,input块的功能已经发生了变化。本月早些时候,引入了input区块发射有效负载的能力。block_actions

块的块工具包参考文档input中,您可以看到一个新dispatch_action参数。这是一个布尔值,当设置为 true 时,会导致块内的交互元素在更改时input发出block_actions有效负载。

因此,为了更直接地回答您的问题,您的#3 解决方案现在应该可以在没有您包含的警告的情况下实现。放置static_select一个input块的内部,并设置dispatch_actiontrue,如下所示:

{
        "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
        }
    }
Run Code Online (Sandbox Code Playgroud)

收到view_submission有效负载后,您现在可以使用正确的验证错误进行响应并将其显示给用户。您仍然可以block_actions根据需要接收有效负载。