I'm building a Registration/Login engine for users.. Everything works fine except I'm using the Compare validation in my Model. When it goes to save my new User entity it gives me this error.
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
I know it's the Compare Validation because If I remove that property, my system works fine.. Also whats a great way to add a DropDown List for my Password Reset Question.. Thank You in advance and Here's my code... Please help..
My Model:
public class User
{
public int UserID { get; set; }
[Required(ErrorMessage = "A User Name is required max 10 characters")]
[StringLength(10)]
public string UserName { get; set; }
[Required(ErrorMessage = "A First Name is required")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name is required")]
public string LastName { get; set; }
[Required]
[DataType(DataType.EmailAddress, ErrorMessage = "Invalid Email Address")]
public string Email { get; set; }
[Required(ErrorMessage = "Password is required")]
[DataType(DataType.Password, ErrorMessage = "Invalid Password")]
public string Password { get; set; }
[Compare("Password")]
[Display(Name = "Confirmation Password")]
public string ConfirmPassword { get; set; }
public string PasswordSalt { get; set; }
[Required(ErrorMessage = "Password Reset Question required")]
public string PasswordHint { get; set; }
[Required(ErrorMessage = "Password Reset Question Answer required")]
public string PasswordHintAnswer { get; set; }
public String UserRole { get; set; }
}
My Controller:
public ActionResult Registration()
{
return View();
}
[HttpPost]
public ActionResult Registration(FacultyScheduler.Models.User user)
{
if (ModelState.IsValid)
{
using (var db = new FacultyContext())
{
var crypto = new SimpleCrypto.PBKDF2();
var encrypPass = crypto.Compute(user.Password);
var sysUser = db.Users.Create();
sysUser.UserName = user.UserName;
sysUser.Email = user.Email;
sysUser.UserID = user.UserID;
sysUser.LastName = user.LastName;
sysUser.FirstName = user.FirstName;
sysUser.Password = encrypPass;
sysUser.ConfirmPassword = user.ConfirmPassword;
sysUser.PasswordSalt = crypto.Salt;
sysUser.PasswordHint = user.PasswordHint;
sysUser.PasswordHintAnswer = user.PasswordHintAnswer;
db.Users.Add(sysUser);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
}
return View(user);
}
This error is not caused by your Compare
attribute - it is a validation exception that is occurring on trying to save your data to the database. You can catch the exception and collate the entity validation errors like this:
try
{
// Your code
}
catch (DbEntityValidatidationException ex)
{
var errors = ex.EntityValidationErrors
.SelectMany(o => o.ValidationErrors)
.Select(o => o.ErrorMessage);
// Log errors out or just debug and inspect them
}
Examine errors
for a collection of messages that will tell you what the problem(s) is/are.
However, since the problem seems to be with the ConfirmPassword
property - the question must be asked; why are you storing this anyway? Do your users really need their password saved in two different columns? Especially since this appears to be a plaintext version of their password, which means your encryption is pointless.