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

[.NET] Return warning when validated inputs don't have an error message #4885

Merged
merged 10 commits into from
Oct 22, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public static FrameworkElement Render(AdaptiveDateInput input, AdaptiveRenderCon
datePicker.Style = context.GetStyle("Adaptive.Input.Date");
datePicker.DataContext = input;

if (input.IsRequired && string.IsNullOrEmpty(input.ErrorMessage))
{
context.Warnings.Add(new AdaptiveWarning((int)AdaptiveWarning.WarningStatusCode.NoErrorMessageForValidatedInput,
"Inputs with validation should include an ErrorMessage"));
Copy link
Member

@shalinijoshi19 shalinijoshi19 Oct 7, 2020

Choose a reason for hiding this comment

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

@RebeccaAnne do we have a plan to localize these error strings that we bubble out via the AdaptiveWarning/Error object(s)? #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's not currently any plan to localize warning strings. I added it to the doc i'm working on that covers possible areas for localization.


In reply to: 501253546 [](ancestors = 501253546)

}

context.InputValues.Add(input.Id, new AdaptiveXceedDateInputValue(input, datePicker));

return datePicker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public static FrameworkElement Render(AdaptiveNumberInput input, AdaptiveRenderC
numberPicker.Style = context.GetStyle("Adaptive.Input.Number");
numberPicker.DataContext = input;

if ((!Double.IsNaN(input.Max) || !Double.IsNaN(input.Min) || input.IsRequired) && string.IsNullOrEmpty(input.ErrorMessage))
{
context.Warnings.Add(new AdaptiveWarning((int)AdaptiveWarning.WarningStatusCode.NoErrorMessageForValidatedInput, "Inputs with validation should include an ErrorMessage"));
}

context.InputValues.Add(input.Id, new AdaptiveXceedNumberInputValue(input, numberPicker));

return numberPicker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public static FrameworkElement Render(AdaptiveTextInput input, AdaptiveRenderCon
textBox.Style = context.GetStyle($"Adaptive.Input.Text.{input.Style}");
textBox.DataContext = input;

if ((input.IsRequired || input.Regex != null) && string.IsNullOrEmpty(input.ErrorMessage))
{
context.Warnings.Add(new AdaptiveWarning((int)AdaptiveWarning.WarningStatusCode.NoErrorMessageForValidatedInput,
"Inputs with validation should include an ErrorMessage"));
}

context.InputValues.Add(input.Id, new AdaptiveXceedTextInputValue(input, textBox));

if (input.InlineAction != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public static FrameworkElement Render(AdaptiveTimeInput input, AdaptiveRenderCon
timePicker.Style = context.GetStyle("Adaptive.Input.Time");
timePicker.DataContext = input;

if (input.IsRequired && string.IsNullOrEmpty(input.ErrorMessage))
{
context.Warnings.Add(new AdaptiveWarning((int)AdaptiveWarning.WarningStatusCode.NoErrorMessageForValidatedInput,
"Inputs with validation should include an ErrorMessage"));
Copy link
Member

Choose a reason for hiding this comment

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

Is there a common place in the shared .NET library that we could house this string as a variable to reference in all these places vs needing to hardcode the literal each place?

}

context.InputValues.Add(input.Id, new AdaptiveXceedTimeInputValue(input, timePicker));

return timePicker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public static FrameworkElement Render(AdaptiveChoiceSetInput input, AdaptiveRend
inputValue = new AdaptiveChoiceSetInputValue(input, uiChoices, uiChoices.Children[0]);
}

if (input.IsRequired && string.IsNullOrEmpty(input.ErrorMessage))
{
context.Warnings.Add(new AdaptiveWarning((int)AdaptiveWarning.WarningStatusCode.NoErrorMessageForValidatedInput,
"Inputs with validation should include an ErrorMessage"));
}

context.InputValues.Add(input.Id, inputValue);

return uiGrid;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Windows;
using System.Windows.Controls;

Expand All @@ -14,6 +15,14 @@ public static FrameworkElement Render(AdaptiveDateInput input, AdaptiveRenderCon
textBox.Style = context.GetStyle($"Adaptive.Input.Text.Date");
textBox.SetContext(input);

DateTime maxDate, minDate;
if ((DateTime.TryParse(input.Max, out maxDate) || DateTime.TryParse(input.Min, out minDate) || input.IsRequired)
&& string.IsNullOrEmpty(input.ErrorMessage))
{
context.Warnings.Add(new AdaptiveWarning((int)AdaptiveWarning.WarningStatusCode.NoErrorMessageForValidatedInput,
"Inputs with validation should include an ErrorMessage"));
}

context.InputValues.Add(input.Id, new AdaptiveDateInputValue(input, textBox));

return textBox;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public static FrameworkElement Render(AdaptiveNumberInput input, AdaptiveRenderC
textBox.Style = context.GetStyle($"Adaptive.Input.Text.Number");
textBox.SetContext(input);

if ((!Double.IsNaN(input.Max) || !Double.IsNaN(input.Min) || input.IsRequired)
&& string.IsNullOrEmpty(input.ErrorMessage))
{
context.Warnings.Add(new AdaptiveWarning((int)AdaptiveWarning.WarningStatusCode.NoErrorMessageForValidatedInput,
"Inputs with validation should include an ErrorMessage"));
}

context.InputValues.Add(input.Id, new AdaptiveNumberInputValue(input, textBox));

return textBox;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Dynamic;
using System.Windows;
using System.Windows.Controls;

namespace AdaptiveCards.Rendering.Wpf
{

public static class AdaptiveTextInputRenderer
{
public static FrameworkElement Render(AdaptiveTextInput input, AdaptiveRenderContext context)
Expand All @@ -29,6 +28,12 @@ public static FrameworkElement Render(AdaptiveTextInput input, AdaptiveRenderCon
textBox.Style = context.GetStyle($"Adaptive.Input.Text.{input.Style}");
textBox.SetContext(input);

if ((input.IsRequired || input.Regex != null) && string.IsNullOrEmpty(input.ErrorMessage))
{
context.Warnings.Add(new AdaptiveWarning((int)AdaptiveWarning.WarningStatusCode.NoErrorMessageForValidatedInput,
"Inputs with validation should include an ErrorMessage"));
}

context.InputValues.Add(input.Id, new AdaptiveTextInputValue(input, textBox));

if (input.InlineAction != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Windows;
using System.Windows.Controls;

Expand All @@ -14,6 +15,14 @@ public static FrameworkElement Render(AdaptiveTimeInput input, AdaptiveRenderCon
textBox.Style = context.GetStyle("Adaptive.Input.Text.Time");
textBox.SetContext(input);

TimeSpan maxTime, minTime;
if ((TimeSpan.TryParse(input.Max, out maxTime) || TimeSpan.TryParse(input.Min, out minTime) || input.IsRequired)
&& string.IsNullOrEmpty(input.ErrorMessage))
{
context.Warnings.Add(new AdaptiveWarning((int)AdaptiveWarning.WarningStatusCode.NoErrorMessageForValidatedInput,
"Inputs with validation should include an ErrorMessage"));
}

context.InputValues.Add(input.Id, new AdaptiveTimeInputValue(input, textBox));

return textBox;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public static FrameworkElement Render(AdaptiveToggleInput input, AdaptiveRenderC
uiToggle.Style = context.GetStyle($"Adaptive.Input.Toggle");
uiToggle.SetContext(input);

if (input.IsRequired && string.IsNullOrEmpty(input.ErrorMessage))
{
context.Warnings.Add(new AdaptiveWarning((int)AdaptiveWarning.WarningStatusCode.NoErrorMessageForValidatedInput,
"Inputs with validation should include an ErrorMessage"));
}

context.InputValues.Add(input.Id, new AdaptiveToggleInputValue(input, uiToggle));

return uiToggle;
Expand Down
7 changes: 6 additions & 1 deletion source/dotnet/Library/AdaptiveCards/AdaptiveWarning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ public enum WarningStatusCode
/// <summary>
/// An input was marked as requiring input, but didn't have a label.
/// </summary>
EmptyLabelInRequiredInput = 14
EmptyLabelInRequiredInput = 14,

/// <summary>
/// An input has validation properties set, but no error message to display to users
/// </summary>
NoErrorMessageForValidatedInput = 15
};

/// <summary>
Expand Down