williamsandonz
williamsandonz

Reputation: 16420

ASP.NET MVC how to get no validation on form for model

I have a registration form and a login form for users. I want validation on the Registration form obviously, but I do not want it on the login form. I've noticed it validates things with JS on the login form. I.E it doesn't even try to submit the form until the user types in a password of length over 5 (I.E what I set in the validation). How do I disable this?

Login form:

@using (Html.BeginForm("Login", "Users", FormMethod.Post, new { @class = "well"})) {

        <legend>Login</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Email, new { @placeholder = "Email" })
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Password, new { @placeholder = "Password" })
        </div>

        <label class="checkbox">
            @Html.CheckBoxFor(model => model.HasRememberMeOn) Remember me
        </label>


        if (ViewData["ErrorMessage"] != null)
        {
                <p>@ViewData["ErrorMessage"]</p>
        }
        <p>
            <input type="submit" value="Login" />
        </p>

}

Model:

[Required]
        [StringLength(20, MinimumLength=5)]
        [Username(ErrorMessage="Username must: Contain at least one letter, no spaces, no special characters")]
        public string Username { get; set; }

        [NotMapped]
        [StringLength(25, MinimumLength = 5)]
        public string Password { get; set; }

        [Required(ErrorMessage="Password is required")]
        public string HashedPassword { get; set; }

        [Required]
        [StringLength(50, MinimumLength = 3)]
        public string Email { get; set; }

        [NotMapped]
        [Compare("Email", ErrorMessage = "Your emails don't match")]
        [DisplayName("Confirm email")]
        public string Email2 { get; set; }

        public Boolean IsActivated { get; set; }

        [DisplayName("Date joined")]
        public DateTime DateCreated { get; set; }

        [Required]
        [StringLength(50, MinimumLength = 2)]
        [DisplayName("Real name")]
        public string Name { get; set; }

Upvotes: 1

Views: 1026

Answers (1)

Marc
Marc

Reputation: 6771

The correct solution would be using another model for the login form without the Required attributes etc. And you need just the username and password for login, so it wouldn't be a good idea to use the registration model anyway.

Upvotes: 4

Related Questions