Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Prevent multiline values in short TextInputs #2789

Merged
merged 4 commits into from
Oct 17, 2023

Conversation

BattleRush
Copy link
Contributor

Description

When creating a Modal TextInput with the style Short and setting the value to a multi-line string, the error received will say just "Invalid Form Body", without exact feedback to the developer what the issue is.

// var description = <multi-line string>

var builder = new ModalBuilder()
{
    Title = "Custom modal",
    CustomId = "custom-id"
};

builder.AddTextInput("Description", "description", placeholder: "Description", value: description, required: true);

var modal = builder.Build();

// send modal

This will cause an error:

The server responded with error 50035: Invalid Form Body
COMPONENT_VALIDATION_FAILED: Component validation failed

Changes

In TextInputBuilder we check if the value set is a multi-line string by checking the presence of \n and if the Style is short we throw a ArgumentException.

Currently it would still be possible to cause this error by doing the following step:

  1. Set Style to Paragraph
  2. Set multi-line string
  3. Change Style to Short

Additional checks could be set if requested to the setter of TextInputStyle property

@BattleRush BattleRush changed the title Prevent multiline values in short Textinputs Prevent multiline values in short TextInputs Oct 13, 2023
@BattleRush BattleRush changed the title Prevent multiline values in short TextInputs [Fix] Prevent multiline values in short TextInputs Oct 13, 2023
@@ -1415,6 +1418,9 @@ public string Value
throw new ArgumentOutOfRangeException(nameof(value), $"Value must not be longer than {MaxLength ?? LargestMaxLength}.");
if (value?.Length < (MinLength ?? 0))
throw new ArgumentOutOfRangeException(nameof(value), $"Value must not be shorter than {MinLength}");
if (Style == TextInputStyle.Short && value?.Contains('\n') == true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This validation shouldn't be placed in the property setter, as it might be used before setting the style of the text input.
It should be added to the Build() method instead, this way we can be sure the style is set to the right value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to Build method

@Misha-133 Misha-133 enabled auto-merge (squash) October 17, 2023 17:42
@Misha-133 Misha-133 merged commit 33e8340 into discord-net:dev Oct 17, 2023
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants