如何验证AdaptiveCard输入控件?

时间:2019-04-09 15:37:57

标签: c# botframework chatbot adaptive-cards

我正在使用Microsoft聊天机器人中的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;

}

1 个答案:

答案 0 :(得分:0)

不建议使用Adaptive Card模式中的isRequired字段。您可以看到当前的架构here

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

自适应卡模式不支持客户端验证。如果要进行客户端验证,则必须编写自己的Direct Line客户端,方法可能是分叉Web Chat存储库。