internal static void ParseAndValidate(
            GovUkViewModel model,
            PropertyInfo property,
            HttpRequest httpRequest)
        {
            string       propertyName    = $"GovUk_Radio_{property.Name}";
            StringValues parameterValues = HttpRequestParameterHelper.GetRequestParameter(httpRequest, propertyName);

            ThrowIfPropertyTypeIsNotNullableEnum(property);
            ParserHelpers.ThrowIfMoreThanOneValue(parameterValues, property);

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

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

                object parameterAsEnum = ParseParameterAsEnum(parameterValueAsString, property);

                property.SetValue(model, parameterAsEnum);
            }

            model.ValueWasSuccessfullyParsed(property);
        }
        internal static void ParseAndValidate(
            GovUkViewModel model,
            PropertyInfo property,
            HttpRequest httpRequest)
        {
            string       propertyName    = $"GovUk_Checkbox_{property.Name}";
            StringValues parameterValues = HttpRequestParameterHelper.GetRequestParameter(httpRequest, propertyName);

            ThrowIfPropertyTypeIsNotListOfEnums(property);

            Type enumType = TypeHelpers.GetGenericTypeFromGenericListType(property.PropertyType);

            ThrowIfAnyValuesAreInvalid(parameterValues, enumType);

            SetPropertyValue(model, property, parameterValues);

            if (IsTooFewSelected(property, parameterValues) ||
                IsTooManySelected(property, parameterValues))
            {
                AddInvalidNumberOfSelectedItemsErrorMessage(model, property, parameterValues);
                return;
            }

            model.ValueWasSuccessfullyParsed(property);
        }
Пример #3
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);
        }
Пример #5
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))
            {
                ParserHelpers.AddRequiredAndMissingErrorMessage(model, property);
                return;
            }

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

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

                property.SetValue(model, parameterValue);
            }

            model.ValueWasSuccessfullyParsed(property);
        }