You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何设置Google Apps Script聊天机器人对话框的textInput必填项

实现Google Apps Script对话框输入字段必填校验

要给对话框中的textInput字段设置必填校验,需要从前端字段标记和后端逻辑校验两部分入手,确保用户必须填写指定字段才能继续操作:

1. 前端标记必填字段

在对话框JSON配置中,给需要必填的textInput对象添加required: true属性,还可以搭配hintText明确提示用户该字段为必填项。修改后的完整配置代码如下:

return {
    "action_response": {
      "type": "DIALOG",
      "dialog_action": {
        "dialog": {
          "body": {
            "sections": [
              {
                "header": "Add new contact",
                "widgets": [
                  {
                    "textInput": {
                      "label": "Name",
                      "type": "SINGLE_LINE",
                      "name": "contactName",
                      "required": true,
                      "hintText": "请输入姓名(必填)"
                    }
                  },
                  {
                    "textInput": {
                      "label": "Address",
                      "type": "MULTIPLE_LINE",
                      "name": "address",
                      "required": true,
                      "hintText": "请输入地址(必填)"
                    }
                  },
                  {
                    "decoratedText": {
                      "text": "Add to favorites",
                      "switchControl": {
                        "controlType": "SWITCH",
                        "name": "saveFavorite"
                      }
                    }
                  },
                  {
                    "decoratedText": {
                      "text": "Merge with existing contacts",
                      "switchControl": {
                        "controlType": "SWITCH",
                        "name": "mergeContact",
                        "selected": true
                      }
                    }
                  },
                  {
                    "buttonList": {
                      "buttons": [
                        {
                          "text": "Next",
                          "onClick": {
                            "action": {
                              "function": "openSequentialDialog"
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            ]
          }
        }
      }
    }
  };

2. 后端逻辑校验(关键)

前端标记仅为基础提示,为避免绕过前端校验,必须在处理提交的函数openSequentialDialog中添加逻辑校验,检查必填字段是否为空:

function openSequentialDialog(e) {
  // 获取对话框提交的表单数据
  const formData = e.parameter;
  
  // 校验必填字段
  const requiredFields = ['contactName', 'address'];
  const missingFields = requiredFields.filter(field => !formData[field] || formData[field].trim() === '');
  
  if (missingFields.length > 0) {
    // 若有必填字段未填,返回提示对话框引导用户修改
    return {
      "action_response": {
        "type": "DIALOG",
        "dialog_action": {
          "dialog": {
            "body": {
              "sections": [
                {
                  "header": "提示",
                  "widgets": [
                    {
                      "textParagraph": {
                        "text": `请填写以下必填字段:${missingFields.join(', ')}`
                      }
                    },
                    {
                      "buttonList": {
                        "buttons": [
                          {
                            "text": "返回修改",
                            "onClick": {
                              "action": {
                                "function": "openInitialDialog"
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              ]
            }
          }
        }
      }
    };
  }
  
  // 校验通过后执行后续业务逻辑
  // ... 此处编写保存联系人等业务代码
}

补充说明

  • 前端required: true会让对话框在用户点击按钮时自动弹出未填提示(不同客户端提示样式可能略有差异)。
  • 后端校验是核心保障,可避免前端校验被绕过的情况,确保业务逻辑的安全性和完整性。

内容的提问来源于stack exchange,提问作者Tom91

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 14:46:02