2

I have a class Question with properties like below:

[Required]
public string Subject { get; set; }
[Required]
public string Content { get; set; }

I try to do some server validation. I've included these scripts:

<script src="@Links.Scripts.jquery_validate_min_js" type="text/javascript"></script>
<script src="@Links.Scripts.jquery_validate_unobtrusive_min_js" type="text/javascript"></script>

And this is the "create" view:

<dt><span>Question title*</span>please be specific but brief</dt>
<dd>@Html.TextBoxFor(m => m.Subject, new { @class = "boxsizingBorder" }) @Html.ValidationMessageFor(m=>m.Subject)</dd>
<dt><span>Question details</span></dt>
<dd>@Html.TextAreaFor(m => m.Content, 7, 30, new { @class = "boxsizingBorder" })@Html.ValidationMessageFor(m => m.Content)</dd>

When i try to post a new question like that:

$('#saveChanges').click(function () {
    $('#createQuestion').submit();
});

(#saveChanges is the id of my span and #createQuestion is the id of my form)

If I don't fill in the example Subject field I momentarily see an error message (field is required, etc.) but question is posted anyway. Is this happening because I'm trying to post the form using jQuery or what?

How should Server Validation work in this instance?

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
Kamil Będkowski
  • 1,092
  • 4
  • 16
  • 36
  • how's the form emitted ? via Html.BeginForm or via standard
    tag ?
    – BigMike Apr 17 '12 at 10:05
  • And if you perform a simple post (with an it does works. I've faced something similar in the past, will dig in my code to see if I find something. (Was something related to call the validation in the submit() function of the form. – BigMike Apr 17 '12 at 10:12
  • Maybe this will solve your issue http://stackoverflow.com/questions/5618247/forcing-a-revalidate-on-mvc3-unobtrusive-remote-validation – BigMike Apr 17 '12 at 10:13
  • Exactly-when i usu "standard" way it works. – Kamil Będkowski Apr 17 '12 at 10:14
  • just add a form submit function issuing the validate() call and return true or false accordingly. $('#form').submit(function() { ..... }); – BigMike Apr 17 '12 at 10:14
  • in here a quite complete example http://stackoverflow.com/questions/5054328/manual-form-validation-in-mvc-3-and-jquery – BigMike Apr 17 '12 at 10:20
  • Yea,but example describes client validation,and i have problem with server validation, – Kamil Będkowski Apr 17 '12 at 10:46
  • Server validation? sorry I've been misleaded by js scripts (they're just for client validation). Can you inspect result of Model.IsValid in your action ? – BigMike Apr 17 '12 at 10:48
  • I'm sorry-that comment should be sent for another person in another forum about another problem:) – Kamil Będkowski Apr 17 '12 at 12:01

1 Answers1

1

You need to actually call Jquery validate before you submit to perform the validation. Something like:

$('#saveChanges').click(function (event) {
   $("#yourForm").validate();
   if ($("#yourForm").valid()) {
       $('#createQuestion').submit();
   }

   event.preventDefault; //this is needed if your saveChanges button is a submit button
});
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • 2
    preventDefault is preferred over return false; – Shyju Apr 17 '12 at 21:31
  • This is an older question but I would suggest using .trigger('submit') instead of .submit. Using the trigger method should invoke the validation where calling .submit will simply submit the form. – Nick Bork May 22 '12 at 17:07
  • @mattytommo While the docs say that .submit is short code for .trigger('submit') I've often found that using trigger will cause validation to fire. Maybe it was an isolated event just in my testing but here is a thread that discusses it: http://stackoverflow.com/questions/645555/should-jquerys-form-submit-not-trigger-onsubmit-within-the-form-tag – Nick Bork May 22 '12 at 17:17
  • @NickBork Hmmm very strange! Something to be wary of I guess. Thanks :) – Mathew Thompson May 22 '12 at 17:31