Custom Validation In Bot V4 Framework Bot Builder 😍 .NET Core

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 

  1. private async Task<bool> MobileNumberValidation(PromptValidatorContext<int> promptcontext, CancellationToken cancellationtoken)    
  2.         {    
  3.             if (!promptcontext.Recognized.Succeeded)    
  4.             {    
  5.                 await promptcontext.Context.SendActivityAsync("Hello, Please enter the valid mobile no",    
  6.                     cancellationToken: cancellationtoken);    
  7.     
  8.                 return false;    
  9.             }    
  10.     
  11.             int count = Convert.ToString(promptcontext.Recognized.Value).Length;    
  12.             if (count != 10)    
  13.             {    
  14.                 await promptcontext.Context.SendActivityAsync("Hello , you are missing some number !!!",    
  15.                     cancellationToken: cancellationtoken);    
  16.                 return false;    
  17.             }    
  18.     
  19.             return true;    
  20.         }    
  21.     
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:
  1. _dialogSet.Add(new TextPrompt(DlgNameId,UserNameValidation));  
  2.             _dialogSet.Add(new NumberPrompt<int>(DlgMobileId,MobileNumberValidation));  
  3.             _dialogSet.Add(new ChoicePrompt(DlgLanguageId,ChoiceValidataion));  
  1. private Task<bool> UserNameValidation(PromptValidatorContext<string> promptContext, CancellationToken cancellationToken)  
  2.        {  
  3.            return Task.FromResult(true);  
  4.        }  
  1. private Task<bool> ChoiceValidataion(PromptValidatorContext<FoundChoice> promptContext, CancellationToken cancellationToken)  
  2.         {  
  3.             return Task.FromResult(true);  
  4.         }  
  1. private async Task<bool> MobileNumberValidation(PromptValidatorContext<int> promptcontext, CancellationToken cancellationtoken)  
  2.         {  
  3.             if (!promptcontext.Recognized.Succeeded)  
  4.             {  
  5.                 await promptcontext.Context.SendActivityAsync("Hello, Please enter the valid mobile no",  
  6.                     cancellationToken: cancellationtoken);  
  7.   
  8.                 return false;  
  9.             }  
  10.   
  11.             int count = Convert.ToString(promptcontext.Recognized.Value).Length;  
  12.             if (count != 10)  
  13.             {  
  14.                 await promptcontext.Context.SendActivityAsync("Hello , you are missing some number !!!",  
  15.                     cancellationToken: cancellationtoken);  
  16.                 return false;  
  17.             }  
  18.   
  19.             return true;  
  20.         }   
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 !!! 🤠


Similar Articles