private static void AddValueIsOutOfRangeErrorMessage(GovUkViewModel model, PropertyInfo property)
        {
            var decimalRangeAttribute = property.GetSingleCustomAttribute <GovUkValidateDecimalRangeAttribute>();

            decimal minimum = SafelyConvertDoubleToDecimal(decimalRangeAttribute.Minimum);
            decimal maximum = SafelyConvertDoubleToDecimal(decimalRangeAttribute.Maximum);

            ParserHelpers.AddErrorMessageBasedOnPropertyDisplayName(model, property,
                                                                    name => $"{name} must be between {minimum} and {maximum}",
                                                                    ErrorMessagePropertyNamePosition.StartOfMessage);
        }
Exemplo n.º 2
0
        public static void ParseAndValidate(
            GovUkViewModel model,
            PropertyInfo property,
            HttpRequest httpRequest)
        {
            string parameterName = $"GovUk_Text_{property.Name}";

            StringValues parameterValues = HttpRequestParameterHelper.GetRequestParameter(httpRequest, parameterName);

            ParserHelpers.ThrowIfMoreThanOneValue(parameterValues, property);
            ParserHelpers.SaveUnparsedValueFromRequestToModel(model, httpRequest, parameterName);

            if (ParserHelpers.IsValueRequiredAndMissingOrEmpty(property, parameterValues, model))
            {
                ParserHelpers.AddRequiredAndMissingErrorMessage(model, property);
                return;
            }

            if (parameterValues.Count > 0)
            {
                string parameterValue = parameterValues[0];

                if (string.IsNullOrWhiteSpace(parameterValue))
                {
                    return; // The value is not required (otherwise we will have exited earlier)
                }

                if (!decimal.TryParse(parameterValue, out decimal parsedDecimalValue))
                {
                    ParserHelpers.AddErrorMessageBasedOnPropertyDisplayName(model, property,
                                                                            (name) => $"{name} must be a number",
                                                                            ErrorMessagePropertyNamePosition.StartOfMessage);
                    return;
                }

                List <GovUkValidationAttribute> customAttributes = property
                                                                   .GetCustomAttributes(typeof(GovUkValidationAttribute))
                                                                   .Cast <GovUkValidationAttribute>()
                                                                   .ToList();
                foreach (var attribute in customAttributes)
                {
                    var result = attribute.CheckForValidationErrors(model, property, parsedDecimalValue);
                    if (result == false)
                    {
                        return;
                    }
                }

                property.SetValue(model, parsedDecimalValue);
            }

            model.ValueWasSuccessfullyParsed(property);
        }
        public static void ParseAndValidate(
            GovUkViewModel model,
            PropertyInfo property,
            HttpRequest httpRequest)
        {
            string parameterName = $"GovUk_Text_{property.Name}";

            StringValues parameterValues = HttpRequestParameterHelper.GetRequestParameter(httpRequest, parameterName);

            ParserHelpers.ThrowIfMoreThanOneValue(parameterValues, property);
            ParserHelpers.SaveUnparsedValueFromRequestToModel(model, httpRequest, parameterName);

            if (ParserHelpers.IsValueRequiredAndMissingOrEmpty(property, parameterValues))
            {
                ParserHelpers.AddRequiredAndMissingErrorMessage(model, property);
                return;
            }

            if (parameterValues.Count > 0)
            {
                string parameterValue = parameterValues[0];

                if (!double.TryParse(parameterValue, out _))
                {
                    ParserHelpers.AddErrorMessageBasedOnPropertyDisplayName(model, property,
                                                                            (name) => $"{name} must be a number",
                                                                            ErrorMessagePropertyNamePosition.StartOfMessage);
                    return;
                }

                int parsedIntValue;
                if (!int.TryParse(parameterValue, out parsedIntValue))
                {
                    ParserHelpers.AddErrorMessageBasedOnPropertyDisplayName(model, property,
                                                                            (name) => $"{name} must be a whole number",
                                                                            ErrorMessagePropertyNamePosition.StartOfMessage);
                    return;
                }

                property.SetValue(model, parsedIntValue);
            }

            model.ValueWasSuccessfullyParsed(property);
        }