Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft Bot framework how to validate date in form flow?

I am working on Microsoft bot framework where i am developing conversation throgh Form Flow . I am asking user to enter 2 dates. I want to perform custom validation logic on date like current date entered by user should be greater than previous date entered by user.

Following is my property for FormFlow

[Prompt("Please enter checkin date")]  
[Pattern(@"^\d{1,2}/\d{1,2}/\d{4}$")]  
public string checkindate { get; set; }  

[Prompt("Please enter checkin date")]  
[Pattern(@"^\d{1,2}/\d{1,2}/\d{4}$")]  
public string checkoutdate { get; set; } 

i want to validate checkoutdate should be greater than checkindate. How i can do this in Bot framework.

Please help me with your suggestion.

Thanks, Ketan

like image 673
ketan Avatar asked Nov 23 '25 06:11

ketan


1 Answers

Validate your checkout field in the BuildForm method using the Validate function in form flow which can be used to validate the fields.

.Field(nameof(checkoutdate),
  validate: async (state, value) =>
  {
     var result = new ValidateResult { IsValid = true, Value = values };
     //Here checkoutdate is present inside value
     //Parse your date in string to Date object
     DateTime checkindate = DateTime.Parse(state.checkindate);
     DateTime checkoutdate = DateTime.Parse(value);

     //If checkoutdate is less than checkin date then its invalid input
     if (checkoutdate < checkindate)
     {
        result.IsValid = false;
        result.Feedback = "Checkout date can't be less than checkin date";
     }
     return result;
 })

Also consider changing your checkout and checkin field types to DateTime, so that any kind of variation of date input will be captured. But in case you need it exactly in the pattern you have specified, then keep it as it is.

Also have a look at Basic features of form flow and Advanced features of form flow, this should help you get familiarized with the concepts of form flow

like image 70
Ashwin Kumar Avatar answered Nov 25 '25 20:11

Ashwin Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!