protected async Task HandleToChangedAsync(ChangeEventArgs changeEventArgs) { bool parsingFailed; validationMessageStore.Clear(toFieldIdentifier); if (HxInputDate <DateTime> .TryParseDateTimeOffsetFromString((string)changeEventArgs.Value, null, out var toDate)) { parsingFailed = false; CurrentValue = Value with { EndDate = toDate?.DateTime }; EditContext.NotifyFieldChanged(toFieldIdentifier); await CloseDropDownAsync(toInputElement); } else { parsingFailed = true; validationMessageStore.Add(toFieldIdentifier, ToParsingErrorMessageEffective); } // We can skip the validation notification if we were previously valid and still are if (parsingFailed || toPreviousParsingAttemptFailed) { EditContext.NotifyValidationStateChanged(); toPreviousParsingAttemptFailed = parsingFailed; } }
public void HxInputDate_TryParseDateTimeOffsetFromString() { // Arrange CultureInfo culture = CultureInfo.GetCultureInfo("cs-CZ"); DateTimeOffset?parsedDateTime; // Act + Assert // empty (valid) Assert.IsTrue(HxInputDate <DateTime> .TryParseDateTimeOffsetFromString(" ", culture, out parsedDateTime)); Assert.AreEqual(null, parsedDateTime); // standard (valid) Assert.IsTrue(HxInputDate <DateTime> .TryParseDateTimeOffsetFromString("10.02.2020", culture, out parsedDateTime)); Assert.AreEqual(new DateTime(2020, 2, 10), parsedDateTime); // whitespaces (valid) Assert.IsTrue(HxInputDate <DateTime> .TryParseDateTimeOffsetFromString(" 20. 3. 2020 ", culture, out parsedDateTime)); Assert.AreEqual(new DateTime(2020, 3, 20), parsedDateTime); // commas (valid) Assert.IsTrue(HxInputDate <DateTime> .TryParseDateTimeOffsetFromString("05,06,2020", culture, out parsedDateTime)); Assert.AreEqual(new DateTime(2020, 6, 5), parsedDateTime); Assert.IsTrue(HxInputDate <DateTime> .TryParseDateTimeOffsetFromString("07,08", culture, out parsedDateTime)); Assert.AreEqual(new DateTime(DateTime.Now.Year, 8, 7), parsedDateTime); // spaces (valid) Assert.IsTrue(HxInputDate <DateTime> .TryParseDateTimeOffsetFromString("05 06 2020", culture, out parsedDateTime)); Assert.AreEqual(new DateTime(2020, 6, 5), parsedDateTime); Assert.IsTrue(HxInputDate <DateTime> .TryParseDateTimeOffsetFromString("07 08", culture, out parsedDateTime)); Assert.AreEqual(new DateTime(DateTime.Now.Year, 8, 7), parsedDateTime); // shortcuts (valid) Assert.IsTrue(HxInputDate <DateTime> .TryParseDateTimeOffsetFromString("0203", culture, out parsedDateTime)); Assert.AreEqual(new DateTime(DateTime.Now.Year, 3, 2), parsedDateTime); Assert.IsTrue(HxInputDate <DateTime> .TryParseDateTimeOffsetFromString("15", culture, out parsedDateTime)); Assert.AreEqual(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 15), parsedDateTime); // time (invalid) Assert.IsFalse(HxInputDate <DateTime> .TryParseDateTimeOffsetFromString("10.02.2020 1:00", culture, out _)); // shortcut, 13th month (invalid) Assert.IsFalse(HxInputDate <DateTime> .TryParseDateTimeOffsetFromString("0113", culture, out _)); // shortcut, 32nd day (invalid) Assert.IsFalse(HxInputDate <DateTime> .TryParseDateTimeOffsetFromString("32", culture, out _)); // time 0:00 unfortunately passes // Assert.IsFalse(HxInputDate<DateTime>.TryParseDateTimeOffsetFromString("10.02.2020 0:00", culture, out _)); }