【问题标题】:How to validate AdaptiveCard input control?如何验证 AdaptiveCard 输入控件?
【发布时间】:2019-05-11 02:54:11
【问题描述】:

我在我的微软聊天机器人中使用 AdaptiveCard 输入表单来获取用户信息。有什么方法可以验证 TextInput 吗?像 asp.net 字段验证器?或 c# 中的任何属性,通过它我可以验证 AdaptiveCard 的所有类型的 TextInput。

var card = new AdaptiveCard()
{
    Body = new List<CardElement>()
    { 
       new TextBlock() { Text = "Email" },
       new TextInput()
       {
           Id = "Email",
           Placeholder = "Enter your email",
           Style = TextInputStyle.Email,
           IsRequired = true
       },


       new TextBlock() { Text = "Mobile" },
       new TextInput()
       {
           Id = "Mobile",
           Placeholder = "+(country code)(Your Phone Number)",
           Style = TextInputStyle.Tel,
           IsRequired = true
       },
    },

    Actions = new List<ActionBase>()
    {
       new SubmitAction()
       {
           Title = "Submit"
       }

   }
};
Attachment attachment = new Attachment()
{
    ContentType = AdaptiveCard.ContentType,
    Content = card
};

从自适应卡片中获取数据MessageReceivedAsync

protected async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
    var message = await result;

    PersonalInfo userinfo = new PersonalInfo();
    if (message.Value != null)
    {
        // Got an Action Submit
        dynamic formvalue = message.Value;

        if (formvalue != null)
        {

            userinfo.Email = GetValue((JObject)formvalue, "Email");
            userinfo.Phone = GetValue((JObject)formvalue, "Mobile");

            var error = GetErrorMessage(userinfo); // Validation

            IMessageActivity replyMessage = context.MakeMessage();

            if (!string.IsNullOrEmpty(error))
            {
                replyMessage.Text = error;
                await context.PostAsync(replyMessage);
            }
            else
            {
                // Save Information in service bus
                if (sbConnectionString != "" && queueName != "")
                    await MainAsync(sbConnectionString, queueName, userinfo);

                replyMessage.Text = "Thank you for taking the time! \n We've submitted your query and an agent will get back to you soon. \n\n Have a great day!";
                await context.PostAsync(replyMessage);
                context.Done(true);
            }

        }
    }
}

// For Validating the Adaptive Card **GetErrorMessage** function pass userinfo

GetErrorMessage(PersonalInfo personalInfo){

    if (string.IsNullOrWhiteSpace(personalInfo.Email)
        || string.IsNullOrWhiteSpace(personalInfo.Phone))
        return "Please fill out all the fields";

    return string.Empty;

}

【问题讨论】:

  • 您可以像验证任何其他文本(例如用户键入的文本)一样验证自适应卡片中的文本。您能否向我们展示一些代码,以便我们了解您到目前为止所做的尝试?
  • var card = new AdaptiveCard() { Body = new List() { new TextBlock() { Text = "Mobile" }, new TextInput() { Id = "Mobile", Speak = "请用国家代码输入您的手机。", Placeholder = "+(国家代码)(你的电话号码)", Style = TextInputStyle.Tel, IsRequired = true } }, Actions = new List () { new SubmitAction() { Title = "Submit", Speak = "Submit" } } };
  • 1.永远不要在 Stack Overflow 评论中发布多行代码。始终在问题本身中发布您的代码。我为你做了,但我必须逐行手动格式化你的代码。如果您通过编辑问题发布了代码,则无需这样做,因为会保留换行符。
  • 2.不要使用 Microsoft.AdaptiveCards 包。请改用 AdaptiveCards 包。当前版本为 1.1.2

标签: c# botframework chatbot adaptive-cards


【解决方案1】:

自适应卡片架构中的 isRequired 字段已弃用。你可以看到当前架构here

您使用的 NuGet 包也已弃用,如您所见 here。请改用this

自适应卡架构不支持客户端验证。如果您想要客户端验证,则必须编写自己的 Direct Line 客户端,可能通过 fork Web Chat repo 来实现。

【讨论】:

    猜你喜欢
    • 2012-04-04
    • 2014-02-17
    • 1970-01-01
    • 2011-02-08
    • 2018-04-16
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 2021-12-11
    相关资源
    最近更新 更多