public static List <ValidationErrorDetail> Validate(AddEditPeopleInControlViewModel model)
        {
            var errorMessages = new List <ValidationErrorDetail>();

            if (String.IsNullOrEmpty(model.PersonInControlName))
            {
                errorMessages.Add(
                    new ValidationErrorDetail
                {
                    ErrorMessage = PersonInControlNameMinLengthError,
                    Field        = "PersonInControlName"
                });
            }
            else if (model.PersonInControlName.Length > 255)
            {
                errorMessages.Add(
                    new ValidationErrorDetail
                {
                    ErrorMessage = PersonInControlNameMaxLengthError,
                    Field        = "PersonInControlName"
                });
            }

            if (!model.DateOfBirthOptional)
            {
                var dobErrorMessages = DateOfBirthAnswerValidator.ValidateDateOfBirth(model.PersonInControlDobMonth, model.PersonInControlDobYear, DobFieldPrefix);
                if (dobErrorMessages.Any())
                {
                    errorMessages.AddRange(dobErrorMessages);
                }
            }

            return(errorMessages);
        }
Ejemplo n.º 2
0
        public static List <ValidationErrorDetail> ValidateTrusteeDatesOfBirth(TabularData trusteesData, List <Answer> answers)
        {
            var errorMessages = new List <ValidationErrorDetail>();

            foreach (var trustee in trusteesData.DataRows)
            {
                var dobMonthKey = $"{trustee.Id}_Month";
                var dobYearKey  = $"{trustee.Id}_Year";
                var dobMonth    = answers.FirstOrDefault(x => x.QuestionId == dobMonthKey);
                var dobYear     = answers.FirstOrDefault(x => x.QuestionId == dobYearKey);
                if (dobMonth == null && dobYear == null)
                {
                    errorMessages.Add(new ValidationErrorDetail
                    {
                        ErrorMessage = DateOfBirthAnswerValidator.MissingDateOfBirthErrorMessage,
                        Field        = dobMonthKey
                    });
                    return(errorMessages);
                }

                var prefix            = trustee.Id + "_";
                var validatorMessages = DateOfBirthAnswerValidator.ValidateDateOfBirth(dobMonth.Value, dobYear.Value, prefix);
                if (validatorMessages.Any())
                {
                    errorMessages.AddRange(validatorMessages);
                }
            }

            return(errorMessages);
        }
Ejemplo n.º 3
0
        private static void EvaluationDateOfBirth(AddEditManagementHierarchyViewModel model, List <ValidationErrorDetail> errorMessages)
        {
            var dobErrorMessages = DateOfBirthAnswerValidator.ValidateDateOfBirth(model.DobMonth, model.DobYear, DobFieldPrefix);

            if (dobErrorMessages.Any())
            {
                errorMessages.AddRange(dobErrorMessages);
            }
        }
Ejemplo n.º 4
0
        public ManagementHierarchyValidator()
        {
            RuleFor(x => x.FirstName)
            .NotEmpty().WithMessage(FirstNameMinLengthError)
            .MaximumLength(255).WithMessage(FirstNameMaxLengthError);

            RuleFor(x => x.LastName)
            .NotEmpty().WithMessage(LastNameMinLengthError)
            .MaximumLength(255).WithMessage(LastNameMaxLengthError);

            RuleFor(x => x.JobRole)
            .NotEmpty().WithMessage(JobRoleMinLengthError)
            .MaximumLength(255).WithMessage(JobRoleMaxLengthError);

            RuleFor(x => x.TimeInRoleMonths)
            .Transform(value => long.TryParse(value, out long val) ? (long?)val : null)
            .NotEmpty().WithMessage(TimeInRoleError)
            .LessThanOrEqualTo(11).WithMessage(TimeInRoleMonthsTooBigError)
            .DependentRules(() =>
            {
                RuleFor(x => x.TimeInRoleYears)
                .Transform(value => long.TryParse(value, out long val) ? (long?)val : null)
                .NotEmpty().WithMessage(TimeInRoleError)
                .LessThanOrEqualTo(99).WithMessage(TimeInRoleYearsTooBigError);
            });

            RuleFor(x => x.IsPartOfOtherOrgThatGetsFunding)
            .NotEmpty().WithMessage(IsPartOfOtherOrgThatGetsFundingError)
            .DependentRules(() =>
            {
                RuleFor(x => x.OtherOrgName)
                .NotEmpty().When(x => x.IsPartOfOtherOrgThatGetsFunding == "Yes").WithMessage(OtherOrgNameError)
                .MaximumLength(750).When(x => x.IsPartOfOtherOrgThatGetsFunding == "Yes").WithMessage(OtherOrgNameLengthError);
            });

            RuleFor(x => x)
            .Custom((viewModel, content) =>
            {
                // maybe move away from this and use Transform trick
                const string DobFieldPrefix = "Dob";
                var dobErrorMessages        = DateOfBirthAnswerValidator.ValidateDateOfBirth(viewModel.DobMonth, viewModel.DobYear, DobFieldPrefix);
                foreach (var error in dobErrorMessages)
                {
                    content.AddFailure(error.Field, error.ErrorMessage);
                }
            });

            RuleFor(x => x.Email)
            .NotEmpty().WithMessage(EmailError)
            .MaximumLength(320).WithMessage(EmailErrorTooLong)
            .Matches(ValidEmailPattern).WithMessage(EmailErrorWrongFormat);

            RuleFor(x => x.ContactNumber)
            .NotEmpty().WithMessage(ContactNumberError)
            .MaximumLength(50).WithMessage(ContactNumberTooLongError);
        }