You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何解决Microsoft Bot Framework FormFlow中字段间的依赖冲突?

Solution for Dependent Country/City Fields in Microsoft Bot Framework FormFlow

Got it, let's fix that dependency conflict between your Country and City fields in FormFlow. The goal is to make sure users only pick valid pairs (France→Paris, Germany→Berlin) by dynamically adjusting options and adding validation checks. Here's how to do it step by step:

1. Dynamic City Options Based on Selected Country

First, we'll make the City field only show relevant options once a Country is chosen. FormFlow's UpdateChoices method lets us adjust available choices based on the current state of the form, so we'll use that to filter City options.

2. Add Validation for Valid Combinations

Even if a user somehow ends up with an invalid pair (like selecting Paris then Germany), we'll add a validation rule to catch that and prompt them to correct their selection. We'll also ensure the City field isn't accessible until a Country is picked, to prevent messy input order.

Full Implementation Code

Here's the complete working code to enforce valid Country-City pairs:

Step 1: Define Your Enums and Form Class

public enum Country { France, Germany }
public enum City { Paris, Berlin }

public class LocationForm
{
    [Prompt("Please select your country: {||}")]
    public Country? Country { get; set; }

    [Prompt("Please select your city: {||}")]
    public City? City { get; set; }
}

Step 2: Configure FormFlow with Dependency Logic

var form = new FormBuilder<LocationForm>()
    .Field(nameof(LocationForm.Country))
    .Field(new FieldReflector<LocationForm>(nameof(LocationForm.City))
        // Update City choices to match selected Country
        .UpdateChoices(async (state, field) =>
        {
            if (state.Country.HasValue)
            {
                return state.Country.Value switch
                {
                    Country.France => new[] { Choice<City>.Create(City.Paris) },
                    Country.Germany => new[] { Choice<City>.Create(City.Berlin) },
                    _ => new List<Choice<City>>()
                };
            }
            // Return empty list if no Country is selected yet
            return new List<Choice<City>>();
        })
        // Validate that City and Country form a valid pair
        .Validate(async (state, value) =>
        {
            var selectedCity = (City)value;
            if (!state.Country.HasValue)
            {
                return new ValidateResult 
                { 
                    IsValid = false, 
                    Feedback = "Please select a country first before choosing a city." 
                };
            }

            bool isPairValid = (state.Country.Value == Country.France && selectedCity == City.Paris) ||
                               (state.Country.Value == Country.Germany && selectedCity == City.Berlin);

            return new ValidateResult
            {
                IsValid = isPairValid,
                Feedback = isPairValid 
                    ? null 
                    : $"Oops, that's an invalid combination! {state.Country.Value} can only be paired with {(state.Country.Value == Country.France ? "Paris" : "Berlin")}."
            };
        })
        // Only show City field after Country is selected
        .SetActive(state => state.Country.HasValue))
    .Build();

How It Works

  • Dynamic Choices: As soon as the user picks a Country, UpdateChoices filters the City options to only the valid one for that Country—no more seeing Berlin when France is selected.
  • Activation Rule: SetActive ensures the City field stays hidden until a Country is chosen, preventing users from making a City selection out of order.
  • Validation Safety Net: The Validate method catches any invalid combinations (even if someone manages to input one via free text) and gives clear, user-friendly feedback to fix it.

内容的提问来源于stack exchange,提问作者Georg Labbé

火山引擎 最新活动