예제 #1
0
        public FormDataValidationResult Validate()
        {
            var result = new FormDataValidationResult();

            if (Title.IsNullOrWhiteSpace())
            {
                result.AddErrorMessage("Title is required");
            }

            if (Description.IsNullOrWhiteSpace())
            {
                result.AddErrorMessage("Description is required");
            }

            if (!IsPublic)
            {
                if (Password.IsNullOrWhiteSpace())
                {
                    result.AddErrorMessage("Password is required");
                }

                if (ConfirmPassword.IsNullOrWhiteSpace())
                {
                    result.AddErrorMessage("Confirm password is required");
                }
                else if (ConfirmPassword != Password)
                {
                    result.AddErrorMessage("Passwords do not match");
                }
            }

            if (StartDate < DateTime.Now)
            {
                result.AddErrorMessage("Invalid Start date");
            }

            if (StartDate >= EndDate)
            {
                result.AddErrorMessage("Invalid start and end date");
            }

            if (Problems.ToList().Count == 0)
            {
                result.AddErrorMessage("1 or more problems must be added");
            }

            // todo check problem uniqueness

            return(result);
        }
예제 #2
0
        public override string Validate(string propertyName)
        {
            if (!DoValidation)
            {
                return(null);
            }

            switch (propertyName)
            {
            case nameof(Login):
                if (Login.IsNullOrWhiteSpace())
                {
                    return(string.Format(Constants.FieldMustBeFilledMessageFormat, nameof(Login)));
                }
                else if (!Login.IsLengthBetween(3, 20))
                {
                    return(string.Format(Constants.LengthErrorMessageFormat, nameof(Login), 3, 20));
                }
                break;

            case nameof(Password):
                if (Password.IsNullOrWhiteSpace())
                {
                    return(string.Format(Constants.FieldMustBeFilledMessageFormat, nameof(Password)));
                }
                else if (!Password.IsLengthBetween(5, 32))
                {
                    return(string.Format(Constants.LengthErrorMessageFormat, nameof(Password), 5, 32));
                }
                break;

            case nameof(ConfirmPassword):
                if (ConfirmPassword.IsNullOrWhiteSpace())
                {
                    return(string.Format(Constants.FieldMustBeFilledMessageFormat, nameof(ConfirmPassword)));
                }
                else if (!ConfirmPassword.IsLengthBetween(5, 32))
                {
                    return(string.Format(Constants.LengthErrorMessageFormat, nameof(ConfirmPassword), 5, 32));
                }
                break;

            case nameof(FirstName):
                if (FirstName.IsNullOrWhiteSpace())
                {
                    return(string.Format(Constants.FieldMustBeFilledMessageFormat, nameof(FirstName)));
                }
                else if (!FirstName.IsLengthBetween(3, 20))
                {
                    return(string.Format(Constants.LengthErrorMessageFormat, nameof(FirstName), 3, 20));
                }
                break;

            case nameof(LastName):
                if (LastName.IsNullOrWhiteSpace())
                {
                    return(string.Format(Constants.FieldMustBeFilledMessageFormat, nameof(LastName)));
                }
                else if (!LastName.IsLengthBetween(3, 20))
                {
                    return(string.Format(Constants.LengthErrorMessageFormat, nameof(LastName), 3, 20));
                }
                break;

            case nameof(Position):
                if (Position == null)
                {
                    return(string.Format(Constants.FieldMustBeFilledMessageFormat, nameof(Position)));
                }
                break;

            case nameof(Birthday):
                if (Birthday.IsNullOrWhiteSpace())
                {
                    return(string.Format(Constants.FieldMustBeFilledMessageFormat, nameof(Birthday)));
                }
                else if (DateTime.Parse(Birthday) < new DateTime(1900, 1, 1) || DateTime.Parse(Birthday) > new DateTime(2005, 1, 1))
                {
                    return(Constants.DateMustBeCorrectMessage);
                }
                break;
            }
            return(null);
        }