I'm trying to disable validation for one spectacular field, but I'm not getting it to work. I made a simple login-system, and I want the users to be able to change their data, without filling out everything. I managed to preload the user variables to the textboxes, so if they want to change, they can swap out the preloaded information with something else, otherwise just let it stay like that. It works OK, but I have a problem with the password fields.
I have added two fields, one for new password, and one for confirm new password. What I want is when the user does not insert anything, it should skip those fields. I think I managed to get the logic on place, but the thing is that I need to insert a new password. Also, the validation. How can I disable the validation for this operation?
My code:
// My Model with Context class.
public class User {
public int id {get; set;}
[Required(ErrorMessage = "Error")]
public string email {get; set;}
[Required(ErrorMessage = "Error")]
public string name {get; set;}
[Required(ErrorMessage = "Error")]
public string password {get; set;}
}
public class dbUser {
public int id {get; set;}
public string email {get; set;}
public string name {get; set;}
public byte[] password {get; set;}
}
ContextClass:
public <dbUser> Users {get; set;}
My controller looks like this:
private UserContext context = new UserContext();
//Preload data in textboxes
public ActionResult Edit(int id = 0)
{
User user = (from p in context.Users
where p.id == id
select new User()
{
email = p.email,
name = p.name
}).FirstOrDefault();
return View(user);
}
//
// POST: /Bruker1/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id, FormCollection form)
{
dbUser changeUser = context.Users.Single(p => p.id == id);
changeUser.email = form["email"];
changeUser.name = form["name"];
if(form["password"].Count() != 0) {
string pass = form["password"];
byte[] newPass = makeNewPass(pass); // crypts the password
changeUser.pass = newPass;
}
context.saveChanges();
return View();
}
This is my View-page:
@model Project.Models.User
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
<fieldset>
<div class="editor-label">
@Html.LabelFor(model => model.email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.email)
@Html.ValidationMessageFor(model => model.email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.name)
@Html.ValidationMessageFor(model => model.name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.password)
</div>
<div class="editor-field">
@Html.PasswordFor(model => model.password)
@Html.ValidationMessageFor(model => model.password)
</div>
</fieldset>
<p>
<input type="submit" value="Save" />
</p>
As you see, I haven't added the method for disabling validation for password field. I tried to remove the validationMessageFor, not working. Tried to use ModelState.Remove("password"), didn't work. No success in getting further.
Any ideas of how I can disable the validation for this field? Thanks.