Customize validation message in Bot framework C#

274 views Asked by At

How to customize validation messages in Bot framework form flow? Below is the sample code I'm working where if the user type any text other than the options provided I need to give them the choice list back saying that the option chosen is not correct.

    [Prompt("Please choose # category... {||}", ChoiceStyle = ChoiceStyleOptions.Buttons)]
    public string Category;

    [Prompt("Please choose  # sub category... {||}", ChoiceStyle = ChoiceStyleOptions.Buttons)]
    public string Subcategory;

    [Prompt("We need some more details to create the request, provide me your **contact number**...")]
    [Pattern(Validations.Phone)]
    public string ContactNumber;

    [Prompt("Please provide **Attachement** if you don't have attachemnt please enter ? {||}")]
    public ChoiceOptions? Attachment;

Currently, if the option is not on the list it simply throws a message like

' Userinput is not a contact/attachment/category option'

1

There are 1 answers

1
Ed Boykin On BEST ANSWER

Jobin, you want to look at the more advanced features of formFlow. You can create business logic around your fields by using field validation functions. This is a sample of what that looks like taken from MS Online docs. You can see the full sample and docs here I find that this method way of creating my form is much more flexible than using attributes.

public static IForm<SandwichOrder> BuildForm()
{
...
return new FormBuilder<SandwichOrder>()
    .Message("Welcome to the sandwich order bot!")
    .Field(nameof(Sandwich))
    .Field(nameof(Bread))
    .Field(nameof(Toppings),
        validate: async (state, value) =>
        {
            var values = ((List<object>)value).OfType<ToppingOptions>();
            var result = new ValidateResult { IsValid = true, Value = values };
            if (values != null && values.Contains(ToppingOptions.Everything))
            {
                result.Value = (from ToppingOptions topping in Enum.GetValues(typeof(ToppingOptions))
                                where topping != ToppingOptions.Everything && !values.Contains(topping)
                                select topping).ToList();
            }
            return result;
        })
    .Message("For sandwich toppings you have selected {Toppings}.")
    ...
    .Build();
}