Before reading this article, I highly recommend you to read the Dialogs in Bot article.
Custom validation
The in-built prompt dialog handles only limited set of the validations like it has to check specific input only (Refer:
Prompt Dialog ). On top of it, how do we add our own custom validation? Let's see how.
Let us start with an example: The PromptNumber dialog checks whether a user enters a number or not ( In-Built Prompt dialog condition). In my requirement, I've to check if the entered number is higher than “10” or not. For example, a mobile number is exact 10 digits. We will apply a custom validation for checking if a mobile number is 10 digits.
This article explains how to implement custom validation for the above case.
Implement Custom validation
The promptvalidatorcontext class is used to implement this custom validation functionality. This class is a generic class. Based on the prompt dialog type, the generic type changes.
The PromptDialog type promptvalidatorcontext class returns the Boolean variable, where,
True: Prompt executes the next step
False: Stay in the same function.
Before implementing custom validations, first, we should find if the InBuilt condition succeeded or not.
How do we know that the inbuilt prompt dialog is a success or not? The promptvalidatorcontext class has a Recognized property (internally class). If it's true, that means the validation has succeeded.
Implement the Custom validation concept
- private async Task<bool> MobileNumberValidation(PromptValidatorContext<int> promptcontext, CancellationToken cancellationtoken)
- {
- if (!promptcontext.Recognized.Succeeded)
- {
- await promptcontext.Context.SendActivityAsync("Hello, Please enter the valid mobile no",
- cancellationToken: cancellationtoken);
-
- return false;
- }
-
- int count = Convert.ToString(promptcontext.Recognized.Value).Length;
- if (count != 10)
- {
- await promptcontext.Context.SendActivityAsync("Hello , you are missing some number !!!",
- cancellationToken: cancellationtoken);
- return false;
- }
-
- return true;
- }
-
In the above code example, I first check if (!promptcontext.Recognized.Succeeded) is true or not. If it's false, give the user a message. If it's true, move to the next step where I check the length of the int.
In the following example, I am checking the count of the number and check for 10. If the number of characters is not 10, then give the user a message and return false. Else, return true.
Calling Custom validation function
Now, we've out custom validator ready. Let's call this function.
In our prompt dialog call, we will pass this function as a callback function. As you can see below, we add it using the Add method. Bot initially invokes this function whenever this prompt dialog is called.
Adding the Custom validation delegate in Dialogset:
- _dialogSet.Add(new TextPrompt(DlgNameId,UserNameValidation));
- _dialogSet.Add(new NumberPrompt<int>(DlgMobileId,MobileNumberValidation));
- _dialogSet.Add(new ChoicePrompt(DlgLanguageId,ChoiceValidataion));
- private Task<bool> UserNameValidation(PromptValidatorContext<string> promptContext, CancellationToken cancellationToken)
- {
- return Task.FromResult(true);
- }
- private Task<bool> ChoiceValidataion(PromptValidatorContext<FoundChoice> promptContext, CancellationToken cancellationToken)
- {
- return Task.FromResult(true);
- }
- private async Task<bool> MobileNumberValidation(PromptValidatorContext<int> promptcontext, CancellationToken cancellationtoken)
- {
- if (!promptcontext.Recognized.Succeeded)
- {
- await promptcontext.Context.SendActivityAsync("Hello, Please enter the valid mobile no",
- cancellationToken: cancellationtoken);
-
- return false;
- }
-
- int count = Convert.ToString(promptcontext.Recognized.Value).Length;
- if (count != 10)
- {
- await promptcontext.Context.SendActivityAsync("Hello , you are missing some number !!!",
- cancellationToken: cancellationtoken);
- return false;
- }
-
- return true;
- }
In the above code, three functions handle the custom validations including the UserNameValidaton and ChoiceValidation. Similarly, you can add more custom validators.
Retry Prompt
Retry Prompt is used to overwrite the previous message. Suppose the in-built validation fails and we want to notify the user with a different message, that time we can use the “RetryPrompt” option with a different message.
You can find the complete sample
here.
Conclusion
I hope you learned how to implement custom validations in Bot framework !!! In my next article, we will see how to apply a custom dialog in Bot Framework.
Happy Reading !!! Happy coding !!! π€