2

I can't get the bootstrap modal and asp.net mvc validation start working together. I've got a complex form with some validation displayed in bootstrap modal. Unfortunetely when I hit the submit button the validation doesn't work at all.

The form uses standard asp.net mvc validation. Below there is a part of it just to get the idea of how it is build:

@using (Html.BuildForm().AddClass("form-horizontal").Id("contact-add-popup").EncType(FormEncType.MultipartData).Begin()) {
@Html.AntiForgeryToken()
@Html.Partial("_Alerts")
<div class="control-group">

<div class="control-group company-field">
    @Html.BuildLabelFor(m => m.Name).AddClass("control-label")
    <div class="controls">
        @Html.BuildTextBoxFor(m => m.Name).AddClass("input-xxlarge")
        @Html.ValidationMessageFor(m => m.Name)
    </div>
</div>
(...)

Here is my modal:

<div id="createContactModal" class="modal hide fade modal-contact" tabindex="-1" role="dialog" aria-labelledby="createContactModalLabel" aria-hidden="true" data-backdrop="static">
<div class="modal-header">
    <h4 class="modal-label" id="createContactModalLabel">Add contact</h4>
</div>
<div class="modal-body">
    @Html.Partial("_CreateContact", new ContactCreateModel())
</div>
<div class="modal-footer">
    <a href="javascript:$('#contact-add-popup').submit();" class="btn btn-primary">Zapisz</a>
    <button class="btn" data-dismiss="modal" aria-hidden="true">Zamknij</button>
</div>

And some javascript that I hope to get the validation working:

        $('#createContactModal').on('shown', function () {
            $("#contact-add-popup").removeData("validator");
            $("#contact-add-popup").removeData("unobtrusiveValidation");
            $.validator.unobtrusive.parse("#contact-add-popup");
        });

        $('#contact-add-popup').on('submit', function(e){
            e.preventDefault();

            $.validator.unobtrusive.parse($("#contact-add-popup"));

            if ($('#contact-add-popup').valid()){
                alert('AJAX');
            }
        }); 

The line if ($('#contact-add-popup').valid()) returns always true. How can I get the modal and validation to work?

6
  • Try to parse the form directly instead of the modal! Commented Nov 11, 2013 at 10:54
  • 2
    What is the extension Html.BuildForm() ? Commented Nov 11, 2013 at 11:10
  • @AndreiMikhalevich it is Build.Mvc Commented Nov 11, 2013 at 11:46
  • @Fals What do you mean by directly? Commented Nov 11, 2013 at 14:33
  • Give an Id to your form an then use the validate function calling for the form Id instead of the modal div. Commented Nov 11, 2013 at 15:45

3 Answers 3

3

You should try this way:

var form = $("#contact-add-popup")
        .removeData("validator")
        .removeData("unobtrusiveValidation");

$.validator.unobtrusive.parse(form);

Stackoverflow: unobtrusive validation not working with dynamic content

Sign up to request clarification or add additional context in comments.

3 Comments

As you may see above I do the same onShown event.
Just placed your code in the document ready event but still no effect.
Thanks man! I missed this in the posters information.. I'm familiar with all sorts of validation issues, and for some reason one modal was having specific problems. This solved it!
3

After some research I found that javascript validation script files were missing - so the client side validation was not working at all. After including these files everything works fine.

Thanks for all answers.

1 Comment

the script is @Scripts.Render("~/bundles/jqueryval")
0

Add this in the base view, load modal and enable client side validation.

@section scripts {

  @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }

  @* The normal bootstrap behavior is to only grab the content
     for the modal once, if you need to pull in different partial
     views then the data on the modal will have to be cleared. *@

  <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>

  <script type="text/javascript">
    $(function () {
      $('#modal-container').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget); // Button that triggered the modal
        var url = button.attr("href");
        var modal = $(this);
        //enable client side validation after page is loaded
        modal.find('.modal-content').load(url, function () {
          $('#registration_form').removeData("validator");
          $('#registration_form').removeData("unobtrusiveValidation");
          $.validator.unobtrusive.parse('#registration_form');
        });
      });
    });
  </script>
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.