I have a signup action that attempts checks if the model state is valid, attempts a sign in, if fail revalidates the model. I am unable to revalidate the model using TryValidateModel
. I have modified some functions to force the model to be invalid.
Here is my sign up action:
[HttpPost]
public IActionResult SignUp(SignUpModel signUp, string returnURL)
{
if (ModelState.IsValid)
{
if (loginManager.SignUp(signUp)) return Ok("Account created");
TryValidateModel(signUp); // breakpoint 1
}
return View(signUp); // breakpoint 3
}
This is the SignUp()
function that forces the invalid state:
public bool SignUp(SignUpModel signUp)
{
signUp.ForceInvalid();
return false;
}
This is the forced function inside SignUpModel
internal void ForceInvalid() => Validation.IsDuplicateEmail = true;
Here is my validation attribute class used for email:
class EmailExistsAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (validationContext.ObjectInstance is SignUpModel signUp && signUp.Validation.IsDuplicateEmail)
new ValidationResult("Email already exists"); // breakpoint 2
return ValidationResult.Success;
}
}
I have used the attribute like this:
[EmailExists]
public string Email { get; set; }
The code breaks at the breakpoints in the order: 1 2 3. Since the code breaks at 2, the model state should be invalid. However at breakpoint 3, the ModelState.IsValid
is still true and it has no errors. I have even tried ModelState.Clear()
even though it doesn't make sense in this context. The output is still the same.