Copied to clipboard

Flag this post as spam?

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


  • Joshua Lemli 52 posts 234 karma points
    Feb 27, 2018 @ 20:09
    Joshua Lemli
    0

    CompareAttribute data annotation causes jQuery error with `BeginUmbracoForm` and client validation

    I am using some code (like the following) to create a form:

    Html.EnableClientValidation();
    Html.EnableUnobtrusiveJavaScript();
    Html.RequiresJs("/umbraco_client/ui/jquery.js");
    Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.min.js");
    Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.unobtrusive.min.js");
    var someModel = new SomeModel();
    using (Html.BeginUmbracoForm<SomeController>("SomeMethod"))
    {
        @Html.ValidationSummary("someModel", true)
    
        <div>
            @Html.LabelFor(m => someModel.RandomProperty)
            @Html.PasswordFor(m => someModel.RandomProperty)
            @Html.ValidationMessageFor(m => someModel.RandomProperty)
        </div>
        <button>Submit</button>
    }
    

    It works, but jQuery throws an error:

    Uncaught Error: Syntax error, unrecognized expression: :input[name=someModel.RandomProperty]
    

    The property that it is referencing looks like this:

            [Required]
            [Display(Name = "Some Prop")]
            [Compare("OtherProp", ErrorMessage = "These should match.")]
            public string RandomProperty { get; set; }
    

    Removing the Compare data attribute "fixes" the error, but I want to be able to use this attribute!

    I will note that nothing breaks... there's just an error in the JS-console. The property is properly "compared" and my c# code keeps-on-goin'.

Please Sign in or register to post replies

Write your reply to:

Draft