Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • AmandaEly 123 posts 379 karma points
    Jan 07, 2016 @ 17:23
    AmandaEly
    0

    Form validation

    Hi,

    I have a form I am using in a partial view (a password change form). I have written out a nice set of jquery validation rules, sitting in a script on the main page (not the partial). This is completely ignored. I now understand that I have to use jQuery unobtrusive validation as well. Is this correct?

    I am unwilling to use Umbraco Forms because (a) I can't afford it b) I don't see why I should need it.

    I enclose my code

    Model public class ChangePasswordModel { [Required] [DisplayName("New Password")] public string NewPassword { get; set; } [Required] [DisplayName("Confirm Password")] public string VerifyPassword { get; set; } } View

    @using ElyRunners.Models.CrudModels @inherits UmbracoTemplatePage @{ Layout = "Master.cshtml"; } @Html.Partial("~/Views/Partials/ChangePassword.cshtml", new ChangePasswordModel())

    <script>
    /* The passwords must match */
    //$(".validation-summary-errors").removeClass("validation-summary-errors");
    //$(".input-validation-error").removeClass("input-validation-error").parent().addClass("has-error");
    $().ready(function() {
        // validate the changePasswordForm
        $("changePasswordForm").validate({
            rules: {
                newPassword : {
                    required:true,
                    minlength: 5
                },
                verifyPassword:{
                    required:true,
                    minlength: 5,
                    equalTo: "#newPassword"
                }
            },
            messages: {
                newPassword: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long"
                },
                verifyPassword: {
                    equired: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long",
                    equalTo: "Please enter the same password as above"
                }
            }
        });
    });
    

    Partial View

    @using ElyRunners.Controllers
    

    @model ElyRunners.Models.CrudModels.ChangePasswordModel

    @using (Html.BeginUmbracoForm

    Controller

    public class ChangePasswordController: SurfaceController
    {
    
       public ActionResult ChangePassword(ChangePasswordModel model)
        {
            var memberService = Services.MemberService;
            var membershipHelper = new MembershipHelper(UmbracoContext.Current);
            var loggedInMember = membershipHelper.GetCurrentMember();
            var member = memberService.GetById(loggedInMember.Id);
            memberService.SavePassword(member,model.NewPassword);
            return Redirect("/");
        }
    
    }
    
  • AmandaEly 123 posts 379 karma points
    Jan 08, 2016 @ 12:24
    AmandaEly
    0

    Got it. It's about relying on Data annotations and enabling client validation in the web.config. Then it all works really well. No need for any jquery validation scripting.

Please Sign in or register to post replies

Write your reply to:

Draft