0

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.

Edited by pritaeas: Moved to ASP.NET

2
Contributors
1
Reply
16
Views
3 Years
Discussion Span
Last Post by LastMitch
0

Any ideas of how I can disable the validation for this field? Thanks.

@Shari_1

I think you might have to read this:

http://msdn.microsoft.com/en-us/library/zsyt68f1%28v=vs.100%29.aspx

There's nothing wrong with the code. The issue might be another file involve. So it's hard to debug a code on my end.

So you might have to read the link I provided. That will give you an idea how to approached it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.