Ejemplo n.º 1
0
        public override bool CheckForValidationErrors <TProperty>(
            GovUkViewModel model,
            PropertyInfo property,
            TProperty parameterValue)
        {
            if (typeof(TProperty) != typeof(string))
            {
                throw new Exception("Parameter value has the wrong type");
            }

            var value = parameterValue as string;

            if (value.Length < 8)
            {
                model.AddErrorFor(property, "Password must be at least 8 characters long");
                return(false);
            }

            if (!value.Any(char.IsLower) || !value.Any(char.IsUpper) || !value.Any(char.IsNumber))
            {
                model.AddErrorFor(property, "Password must have at least one lower case letter, one capital letter and one number");
                return(false);
            }

            if (value.Contains("password"))
            {
                model.AddErrorFor(property, "Enter a password that doesn't contain 'password'");
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public static void AddErrorMessageBasedOnPropertyDisplayName(
            GovUkViewModel model,
            PropertyInfo property,
            Func <string, string> getErrorMessageBasedOnPropertyDisplayName,
            ErrorMessagePropertyNamePosition position)
        {
            var displayNameForErrorsAttribute = property.GetSingleCustomAttribute <GovUkDisplayNameForErrorsAttribute>();

            string errorMessage;

            if (displayNameForErrorsAttribute != null)
            {
                switch (position)
                {
                case ErrorMessagePropertyNamePosition.StartOfMessage:
                    errorMessage = getErrorMessageBasedOnPropertyDisplayName(displayNameForErrorsAttribute.NameAtStartOfSentence);
                    break;

                case ErrorMessagePropertyNamePosition.WithinMessage:
                    errorMessage = getErrorMessageBasedOnPropertyDisplayName(displayNameForErrorsAttribute.NameWithinSentence);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(position), position, null);
                }
            }
            else
            {
                errorMessage = getErrorMessageBasedOnPropertyDisplayName(property.Name);
            }

            model.AddErrorFor(property, errorMessage);
        }
Ejemplo n.º 3
0
        private static void AddInvalidNumberOfSelectedItemsErrorMessage(
            GovUkViewModel model,
            PropertyInfo property,
            StringValues parameterValues)
        {
            var responsesRangeAttribute       = property.GetSingleCustomAttribute <GovUkValidateCheckboxNumberOfResponsesRangeAttribute>();
            var displayNameForErrorsAttribute = property.GetSingleCustomAttribute <GovUkDisplayNameForErrorsAttribute>();

            string propertyNameForErrorMessage = displayNameForErrorsAttribute?.NameWithinSentence ?? property.Name;

            string errorMessage;

            if (IsTooFewSelected(property, parameterValues))
            {
                if (parameterValues.Count == 0 &&
                    responsesRangeAttribute.ErrorMessageIfNothingSelected != null)
                {
                    errorMessage = responsesRangeAttribute.ErrorMessageIfNothingSelected;
                }
                else
                {
                    errorMessage = $"Select at least {responsesRangeAttribute.MinimumSelected} " +
                                   $"options for {propertyNameForErrorMessage}";
                }
            }
            else // Implicitly, the user must have selected too many options
            {
                errorMessage = $"Select at most {responsesRangeAttribute.MaximumSelected} " +
                               $"options for {propertyNameForErrorMessage}";
            }

            model.AddErrorFor(property, errorMessage);
        }
Ejemplo n.º 4
0
        public static void AddRequiredAndMissingErrorMessage(GovUkViewModel model, PropertyInfo property)
        {
            var requiredAttribute   = property.GetSingleCustomAttribute <GovUkValidateRequiredAttribute>();
            var requiredIfAttribute = property.GetSingleCustomAttribute <GovUkValidationRequiredIfAttribute>();

            var displayNameForErrorsAttribute = property.GetSingleCustomAttribute <GovUkDisplayNameForErrorsAttribute>();

            string requiredErrorMessage = requiredAttribute != null
                ? requiredAttribute.ErrorMessageIfMissing
                : requiredIfAttribute?.ErrorMessageIfMissing;

            string errorMessage;

            if (requiredErrorMessage != null)
            {
                errorMessage = requiredErrorMessage;
            }
            else if (displayNameForErrorsAttribute != null)
            {
                errorMessage = $"{displayNameForErrorsAttribute.NameAtStartOfSentence} is required";
            }
            else
            {
                errorMessage = $"{property.Name} is required";
            }

            model.AddErrorFor(property, errorMessage);
        }
        public override bool CheckForValidationErrors <TProperty>(GovUkViewModel model, PropertyInfo property, TProperty parameterValue)
        {
            var attribute = GetAttribute <GovUkValidationRegularExpressionAttribute>(property);

            var  value   = parameterValue?.ToString();
            bool matches = Matches(attribute.Pattern, value);

            if (matches)
            {
                return(true);
            }

            model.AddErrorFor(property, attribute.ErrorMessage);
            return(false);
        }
        private static void AddExceedsCharacterCountErrorMessage(GovUkViewModel model, PropertyInfo property)
        {
            var characterCountAttribute       = property.GetSingleCustomAttribute <GovUkValidateCharacterCountAttribute>();
            var displayNameForErrorsAttribute = property.GetSingleCustomAttribute <GovUkDisplayNameForErrorsAttribute>();

            string errorMessage;

            if (displayNameForErrorsAttribute != null)
            {
                errorMessage = $"{displayNameForErrorsAttribute.NameAtStartOfSentence} must be {characterCountAttribute.MaxCharacters} characters or fewer";
            }
            else
            {
                errorMessage = $"{property.Name} must be {characterCountAttribute.MaxCharacters} characters or fewer";
            }

            model.AddErrorFor(property, errorMessage);
        }
        public static void AddErrorMessageBasedOnPropertyDisplayName(
            GovUkViewModel model,
            PropertyInfo property,
            Func <string, string> getErrorMessageBasedOnPropertyDisplayName)
        {
            var displayNameForErrorsAttribute = property.GetSingleCustomAttribute <GovUkDisplayNameForErrorsAttribute>();

            string errorMessage;

            if (displayNameForErrorsAttribute != null)
            {
                errorMessage = getErrorMessageBasedOnPropertyDisplayName(displayNameForErrorsAttribute.NameWithinSentence);
            }
            else
            {
                errorMessage = getErrorMessageBasedOnPropertyDisplayName(property.Name);
            }

            model.AddErrorFor(property, errorMessage);
        }
Ejemplo n.º 8
0
        public static void AddRequiredAndMissingErrorMessage(GovUkViewModel model, PropertyInfo property)
        {
            var requiredAttribute             = property.GetSingleCustomAttribute <GovUkValidateRequiredAttribute>();
            var displayNameForErrorsAttribute = property.GetSingleCustomAttribute <GovUkDisplayNameForErrorsAttribute>();

            string errorMessage;

            if (requiredAttribute.ErrorMessageIfMissing != null)
            {
                errorMessage = requiredAttribute.ErrorMessageIfMissing;
            }
            else if (displayNameForErrorsAttribute != null)
            {
                errorMessage = $"Select {displayNameForErrorsAttribute.NameWithinSentence}";
            }
            else
            {
                errorMessage = $"Select {property.Name}";
            }

            model.AddErrorFor(property, errorMessage);
        }