1

I'm using ASP.NET MVC. In cshtml file I using validator plugin for validation fields. Validation happens on a simple field. Is is code field in cshtml file:

  Html.Bootstrap().FormGroup().TextBoxFor(model => model.EAN).Label()).Class("col-lg-6")).Class("row")

ViewModel field code:

[Display(Name = "Kod EAN")]
[RegularExpression("[0-9]{13}",ErrorMessage = "Kod EAN powinien zawierać 13 znaków")]
 public string EAN { get; set; }

In View i render script:

Scripts.Render("~/bundles/jqueryval")

I use Ajax to search and add information for my fields.This is my Ajax POST

$(document).ready(function () {
function pagination() {
    $('#result-table').each(Utils.Pagination);
}


function getData(id) {
    $.ajax({
        url: "GetSpecific",
        dataType: "json",
        method: "POST",
        cache: false,
        data: {
            id: id
        }
    }).success(function (result) {
        if (result == null) return;
        for (var propName in result) {
            $(".panel [Name$='." + propName + "']").val(result[propName]);
        }
        clear();
    });
}

function clear() {
    $("#result-table").html("");
    $(".modal input").val("");
    $(".pager").remove();
}

function search() {
    var form = $("#search-entry-form :input").serialize();

    $.ajax({
        url: $('#search-entry-form form').attr('action'),
        dataType: "html",
        method: "POST",
        cache: false,
        data: form

    }).success(function (result) {
        $("#result-table").html(result);
        $(".select-specific").on("click", function () { getData($(this).data("specific")) });
        pagination();
    });
}

$("#search-specific").on("click", search);

});

When i try add value for field, everything work fine. The first problem was validate hidden field. I added setDefaults to my validator. This is setDefaults option for hidden validation:

 $.validator.setDefaults({
    ignore: "",
});

But after adding hidden validate after Ajax POST call, validation doesn't work. After Ajax I see message validation field but submit work.

Paweł Groński
  • 567
  • 1
  • 6
  • 17
  • where is the validation code? Which field(s) are you trying to validate? After which of the 2 POST calls you've shown does this problem occur? How is the form submit being triggered? This question could be a lot clearer. – ADyson Jan 16 '17 at 14:38
  • Hello. I added more information. I hope this will be enough. – Paweł Groński Jan 18 '17 at 08:10
  • ok I see. I think maybe the validation does not run client-side because you don't handle the submit event of the form (which triggers the validation), instead you use a "click" on a button within it. These answer smight help you structure your javascript better: http://stackoverflow.com/questions/35362106/how-to-in-asp-net-mvc-prevent-jquery-ajax-submit-if-validation-fails and http://stackoverflow.com/questions/28987752/jquery-post-and-unobtrusive-ajax-validation-not-working-mvc-4 – ADyson Jan 18 '17 at 09:02
  • Thanks for your answer, my friend. I read about .valid() method. The problem is that my form can by empty, can be written by user and can take data from Ajax call. I have modal windows for searching data and than I can click for return data to my form. Then I can accept view. Do you mean to run validation, when adding data to a form or a accept all page? – Paweł Groński Jan 18 '17 at 09:59
  • perhaps I haven't fully understood still. This "EAN" variable, is it within the "#search-entry-form"? Are you trying to validate it before running the "search" function? – ADyson Jan 18 '17 at 10:08
  • OK. I'm add $("#form1").valid() and this worked. But now hidden validate doesn't work. – Paweł Groński Jan 18 '17 at 10:09
  • I don't understand. When I try add setDefoult i take in FireBug "typeerror: validator is undefined" – Paweł Groński Jan 18 '17 at 10:28
  • as far as I know the correct way to set the validator to allow it to validate hidden fields is like this `$.validator.setDefaults({ ignore: [] });` That's what I have used in the past. You have to make sure this doesn't run until everything is loaded on the page, but it must run before you try and run the `.valid()` method. – ADyson Jan 18 '17 at 10:42
  • Hello my friend. After long manipulation of my code I found a strange problem: After show Modal Window validator doesn't work. The model window may not contain the functionality. What you think about this problem? – Paweł Groński Jan 19 '17 at 09:32
  • it's difficult for me to say. If you have been changing the code then I don't know what is happening now, I wouldn't like to say what's wrong with it based on a small description. Perhaps if you edit the question or start a new one with your new problem it will be easier. – ADyson Jan 19 '17 at 09:44
  • You're right man. – Paweł Groński Jan 19 '17 at 09:46
  • Hello ADyson. I created the new question. http://stackoverflow.com/questions/41745705/after-show-modal-window-jquery-validation-plugin-doesnt-work Maybe it will be easier for understanding my problem. – Paweł Groński Jan 20 '17 at 07:52

0 Answers0