internal static string GetModelStateErrorMessage(DateInputParseErrors parseErrors, ModelMetadata modelMetadata) { // TODO Make these messages configurable // (see Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.ModelBindingMessageProvider) Debug.Assert(parseErrors != DateInputParseErrors.None); Debug.Assert(parseErrors != (DateInputParseErrors.MissingDay | DateInputParseErrors.MissingMonth | DateInputParseErrors.MissingYear)); var displayName = modelMetadata.DisplayName ?? modelMetadata.PropertyName; var missingComponents = new List <string>(); if ((parseErrors & DateInputParseErrors.MissingDay) != 0) { missingComponents.Add("day"); } if ((parseErrors & DateInputParseErrors.MissingMonth) != 0) { missingComponents.Add("month"); } if ((parseErrors & DateInputParseErrors.MissingYear) != 0) { missingComponents.Add("year"); } if (missingComponents.Count > 0) { Debug.Assert(missingComponents.Count <= 2); return($"{displayName} must include a {string.Join(" and ", missingComponents)}"); } return($"{displayName} must be a real date"); }
public bool TryGetErrorsForModel(string modelName, out DateInputParseErrors errors) { if (modelName is null) { throw new ArgumentNullException(nameof(modelName)); } return(_errors.TryGetValue(modelName, out errors)); }
public void SetErrorsForModel(string modelName, DateInputParseErrors parseErrors) { if (modelName is null) { throw new ArgumentNullException(nameof(modelName)); } _errors[modelName] = parseErrors; }
public void Parse_InvalidDate_ComputesExpectedParseErrors( string day, string month, string year, DateInputParseErrors expectedParseErrors) { // Arrange // Act var result = DateInputModelBinder.Parse(day, month, year, out var dateComponents); // Assert Assert.Equal(default, dateComponents);
public void GetModelStateErrorMessage(DateInputParseErrors parseErrors, string expectedMessage) { // Arrange var modelMetadata = new DisplayNameModelMetadata("Date of birth"); // Act var result = DateInputModelBinder.GetModelStateErrorMessage(parseErrors, modelMetadata); // Assert Assert.Equal(expectedMessage, result); }
public static DateInputErrorComponents GetErrorComponents(this DateInputParseErrors parseErrors) => DateInputErrorComponents.None | (((parseErrors & (DateInputParseErrors.MissingDay | DateInputParseErrors.InvalidDay)) != 0) ? DateInputErrorComponents.Day : 0) | (((parseErrors & (DateInputParseErrors.MissingMonth | DateInputParseErrors.InvalidMonth)) != 0) ? DateInputErrorComponents.Month : 0) | (((parseErrors & (DateInputParseErrors.MissingYear | DateInputParseErrors.InvalidYear)) != 0) ? DateInputErrorComponents.Year : 0);
public virtual bool TryCreateModelFromErrors(Type modelType, DateInputParseErrors errors, out object?model) { model = default; return(false); }