コード例 #1
0
        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");
        }
コード例 #2
0
        public bool TryGetErrorsForModel(string modelName, out DateInputParseErrors errors)
        {
            if (modelName is null)
            {
                throw new ArgumentNullException(nameof(modelName));
            }

            return(_errors.TryGetValue(modelName, out errors));
        }
コード例 #3
0
        public void SetErrorsForModel(string modelName, DateInputParseErrors parseErrors)
        {
            if (modelName is null)
            {
                throw new ArgumentNullException(nameof(modelName));
            }

            _errors[modelName] = parseErrors;
        }
コード例 #4
0
        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);
コード例 #5
0
        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);
        }
コード例 #6
0
 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);
コード例 #7
0
 public virtual bool TryCreateModelFromErrors(Type modelType, DateInputParseErrors errors, out object?model)
 {
     model = default;
     return(false);
 }