问题
I have below formbuilder code in a class, I want to prompt user based on its response. if user response for field "ConfirmISTTimeZone" is "Yes" then it should prompt for the field "TimeZone" else it should directly prompt "Duration" How to do this?
var formflow = new FormBuilder<FlightBooking>().Message("Tell me Flight details!")
.Field(nameof(Flightname))
.Field(nameof(StartDate), validate: ValidateStartDate)
.Field(nameof(ConfirmISTTimeZone), validate: ValidateResponseOfTimeZone)
.Field(nameof(TimeZone), validate: ValidateTimeZone)
.Field(nameof(Duration), validate: ValidateDuration)
回答1:
You can use the SetActive
method for that on the FieldReflector
class.
var formflow = new FormBuilder<FlightBooking>().Message("Tell me Flight details!")
.Field(nameof(Flightname))
.Field(nameof(StartDate), validate: ValidateStartDate)
.Field(nameof(ConfirmISTTimeZone), validate: ValidateResponseOfTimeZone)
.Field(new FieldReflector<FlightBooking>(nameof(TimeZone))
.SetActive(CheckActiveMethod)
.SetValidate(ValidateTimeZone))
.Field(nameof(Duration), validate: ValidateDuration)
The CheckActiveMethod
is just a method that takes the state and return either true, if the field is active and should be prompted, or false, if the field can be skipped.
回答2:
Another option is include 'active: ActiveTimeZone' in the Field of TimeZone
.Field(nameof(TimeZone), active: ActiveTimeZone, validate: ValidateTimeZone)
private bool ActiveTimeZone(FlightBooking state)
{
bool setActive = true;
if (!state.ConfirmISTTimeZone.Contains("Add ur condition here"))
setActive = false;
return setActive;
}
来源:https://stackoverflow.com/questions/44126219/i-want-to-prompt-further-field-based-on-its-previous-field-result-in-bot-framewo