1
Hello!
I have developed some user registration validations that follow:
using FluentValidation;
using System.Linq;
using System.Text.RegularExpressions;
namespace SaleTheaterTickets.Models.ViewModelValidators
{
public class RegisterViewModelValidator : AbstractValidator<RegisterViewModel>
{
public RegisterViewModelValidator()
{
RuleFor(x => x.Email)
.NotEmpty().WithMessage("Informe o e-mail")
.EmailAddress().WithMessage("Digite um e-mail válido ([email protected])");
RuleFor(x => x.Password)
.NotEmpty().WithMessage("Informe a senha")
.Length(6, 20).WithMessage("Senha deve ter no mínimo 6 e no máximo 20 caractéres")
.Must(RequireDigit).WithMessage("Senha deve ter pelo menos 1 número")
.Must(RequiredLowerCase).WithMessage("Senha deve ter pelo menos 1 caracter minúsculo")
.Must(RequireUppercase).WithMessage("Senha deve ter pelo menos 1 caracter maiúsculo")
.Must(RequireNonAlphanumeric).WithMessage("Digite pelo menos 1 caracter especial (@ ! & * ...)");
RuleFor(x => x.ConfirmPassword)
.NotEmpty().WithMessage("Confirme a senha")
.Equal(x => x.Password).WithMessage("Senhas não conferem");
}
private bool RequireDigit(string password)
{
if (password.Any(x => char.IsDigit(x)))
{
return true;
}
return false;
}
private bool RequiredLowerCase(string password)
{
if (password.Any(x => char.IsLower(x)))
{
return true;
}
return false;
}
private bool RequireUppercase(string password)
{
if (password.Any(x => char.IsUpper(x)))
{
return true;
}
return false;
}
private bool RequireNonAlphanumeric(string password)
{
//if (password.Any(x => char.IsSymbol(x)))
//{
// return true;
//}
//return false;
if (Regex.IsMatch(password, "(?=.*[@#$%^&+=])"))
{
return true;
}
return false;
}
}
}
My problem is being the following; the methods that are called in the property password
Must does not accept null value. When I put something empty in the password, there in the form it seems that Notempty does not work, because it leaves it empty and does not show the message, ending up giving the following error on the server:
An untreated exception occurred while processing the request.
Argumentnullexception: Value cannot be null. (Source parameter) System.Linq.Throwhelper.Throwargumentnullexception (Exceptionargument argument) Stack Query Cookies Headers Forwarding Argumentnullexception: Value cannot be null. (Source parameter) System.Linq.Throwhelper.Throwargumentnullexception (Exceptionargument argument) System.Linq.Enumerable.Any (IEnumerable source, Func <TSource, bool> predicado) SaleTheaterTickets.Models.ViewModelValidators.RegisterViewModelValidator.RequireDigit (string senha) em RegisterViewModelValidator.cs+ if (password.Any (x => char.Isdigit (x))) Defaultvalidatorextensions + <> c__DisplayClass21_0 <T, Tproperty>. b__0 (T x, Tproperty val) in Defaultvalidatorextensions.Cs Fluentvalidation.Defaultvalidatorextensions + <> c__DisplayClass22_0 <T, Tproperty>. b__0 (T x, Tproperty val, Propertyvalidatorcontext propertyValidatorContext) in Defaultvalidatorextensions.Cs
This error happens exactly because the methods below do not accept null value - I’m not sure why either. Could someone help me understand why Notempty doesn’t slash it? Because the Email and Confirmpassword property works.