Oct 5, 2018

JQuery Validation on dynamic form (ASP.NET MVC unobtrusive)

Update

Just add this line in your injected form or in success callback:

              $.validator.unobtrusive.parse("form");              

Bellow code is not needed but it may be useful for its context...

For some reason I've could only make it work on explicit input event binding.

    $(document).off('change', 'input', onInputChange);
        $(document).on('change', 'input', onInputChange);
 function onInputChange() {   
        //Manually initiate unobtrusive validation so error messages are shown
        $(this).closest("form").valid();     
    }

If you are saving using Ajax then prevent saving something like this:

    function SaveItemCallback(e) {
        e.preventDefault();
        if ($(this).closest('form').valid() === false) {
            return;
        }
 var postBody = $(this).closest('form').serialize();
        $.post(options.urlSave, postBody, function (id) {
            loadDetails(id);
        })
            .fail(function () {
                toastr.error(myErrorText);
            });

No comments:

Post a Comment