简体   繁体   中英

Check if an input form is filled in a Adaptive Card bot framework c#

Can we check whether the input form in an adaptive card is filled or not with a warning message. I am currently using an adaptive card to gather user input in bot application,I have already added isRequired for input validation but it doesnot give any warning message instead when I click on submit it doesnot go to the next method. As soon as the user presses submit I want to make sure that the form is not empty

If you have an Adaptive Card like this (notice the ID given to the input):

var card = new AdaptiveCard
{
    Body =
    {
        new AdaptiveTextBlock("Adaptive Card"),
        new AdaptiveTextInput { Id = "text" },
    },
    Actions = {
        new AdaptiveSubmitAction { Title = "Submit" } },
    },
};

You can validate the value sent through the submit action like this:

if (string.IsNullOrEmpty(turnContext.Activity.Text))
{
    dynamic value = turnContext.Activity.Value;
    string text = value["text"];   // The property will be named after your input's ID
    var emailRegex = new Regex(@"^\S+@\S+$");   // This is VERY basic email Regex. You might want something different.

    if (emailRegex.IsMatch(text))
    {
        await turnContext.SendActivityAsync($"I think {text} is a valid email address");
    }
    else
    {
        await turnContext.SendActivityAsync($"I don't think {text} is a valid email address");
    }
}

Validating email with regex can get very complicated and I've taken a simple approach. You can read more about email Regex here: How to validate an email address using a regular expression?

I took totally different approach than the accepted answer here. If you are going to use adaptive cards a lot in your bot than it makes sense to create card models and have validation attributes applied to each field that needs validation. Create custom card prompt inheriting from Prompt<object> class. Override OnPromptAsync and OnRecognizeAsync and check the validation of each field there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM