Reputation: 1052
I Have set two validation on model property using Data Annotation, like below:
[MinLength(8, ErrorMessage = "Password Requires at least one letter, one number and 8 characters long")]
[Required(ErrorMessage = "Password Required")]
public string Password { get; set; }
I want partial validation in some special cases. For e.g i dont want to check minimum length when user login , but only at a time of register.
Can any body know how to achieve this?
Upvotes: 1
Views: 374
Reputation: 2298
Use different ViewModels for Registration and Login, just like the default asp.net-mvc implementation does.
You'll end up with 3 classes: Your Model itself, A login class and a registration class. The only class with the password lenght validation should be the Model itself, not the view Models. Using your controller your should then populate from the ViewModel into the Model(When doing Posts) or from the Model into the ViewModel (When doing Gets)
Example of a Login ViewModel (Taken from the default MVC code)
public class LoginViewModel
{
[Required]
[Display(Name = "Email")]
[EmailAddress]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
And a Register ViewModel, also from the default MVC
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
Example of usage of the Register ViewModel and the Model itself, all default MVC
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
Upvotes: 1