Search code examples
c#botframeworkadaptive-cards

How to control the sum of input values ​on an adaptivecard


I would like to control so that the sum of the numbers entered in the two Input.Number is less than 9. For example, if you enter 5 in A Input.Number when you are between 0 and 9, B Input.Number allows you to enter up to 4. Or, if there is a value from 0 to 9 in Input.Choiceset, if I select 5 in A, I need to be able to select only 0 ~ 4 automatically.

And Below is my code that does not work as intended.

                card.Body.Add(new AdaptiveTextBlock() 
                {
                    HorizontalAlignment = AdaptiveHorizontalAlignment.Left,
                    Spacing = AdaptiveSpacing.None,
                    Size = AdaptiveTextSize.Small,
                    Weight = AdaptiveTextWeight.Bolder,
                    Color = AdaptiveTextColor.Accent,
                    Text = "A"
                });

                card.Body.Add(new AdaptiveNumberInput()
                {
                    Id = "AInput",
                    Spacing = AdaptiveSpacing.None,
                    Placeholder = "0",
                    Min = 0,
                    Max = maxNum - BNum
                });
                //I want to put the value that the user chooses into AResult.
                card.Body.Add(new AdaptiveTextBlock()
                {
                    HorizontalAlignment = AdaptiveHorizontalAlignment.Left,
                    Spacing = AdaptiveSpacing.None,
                    Size = AdaptiveTextSize.Small,
                    Weight = AdaptiveTextWeight.Bolder,
                    Color = AdaptiveTextColor.Accent,
                    Text = "B"
                });
                card.Body.Add(new AdaptiveNumberInput()
                {
                    Id = "BInput",
                    Spacing = AdaptiveSpacing.None,
                    Placeholder = "0",
                    Min = 0,
                    Max = maxNum - ANum
                });

I would really appreciate it if you could help me.


Solution

  • If you look at the InputNumber Schema, you can see this about Min/Max:

    enter image description here

    I'm not sure which clients actually use those for validation, but I'm guessing most don't. There's also a feature request for this, but it doesn't seem to be getting much traction.

    Thankfully, you can do this on your own. It just requires extra work on your end. Bear in mind, it isn't easy.

    1. Use the ActivityPrompt

    You'll notice, however, that this is currently an abstract class, so you can't use it directly. You'll want to write your own class that extends the abstract class.

    2. Write a Validator

    You'd have to do your own validation that checks to see if input is within the min/max values. If not, send a message to tell the user what's wrong and then re-prompt.

    Here's an example of how to validate in the next step.

    You can also pass in a validator to ActivityPrompt. This should get you started:

    AddDialog(new MyActivityPrompt(nameof(TextPrompt), Validator));
    
    [...]
    
    private Task<bool> Validator(PromptValidatorContext<string> promptContext, CancellationToken cancellationToken)
    {
        return Task.FromResult(true);
    }
    

    The real trick would be getting your min/max settings from the card passed into the validator.

    For that, you'd need to add additional data to your AdaptiveCard.Action.Submit.data. You could do something like this:

    var card = new AdaptiveCard();
    var action = new AdaptiveSubmitAction()
    {
        Data = new {
            AInputMin = 0,
            AInputMax = 5,
        }
    };
    card.Actions.Add(action);
    

    Then, when the user clicks submit, the card min/max settings will show up along with the result.

    I also wrote an AdaptiveCardPrompt that does some of this for you, but it's in TypeScript and not in the Bot Framework SDK yet.