I'm doing something similar to the remote validation, except that I already make my calls manually via jquery and setup whatever I had to setup.
Now my problem is, if I want to tell the validator that a particular textbox is not valid (and prevents the page from submitting, highlight the textbox, etc). How would I do this from code?
@Html.LabelFor(m => Model.Slug)
@Html.TextBoxFor(m => Model.Slug)
<span id="UrlMsg" class="field-validation-error" style="display: none;"></span>
if (error) {
$('#UrlMsg').html('This name is already in use.').fadeIn('fast');
//what should I do here for the rest of the validation?
}
This is an improvement on FRL's answer:
JS Function
Call it
First, you can add a validation summary:
Then you can manually trigger jQuery validation / unobtrusive validation with the .showError method. Here is a sample:
Using showErrors did not allow the error to persist, so that running .valid() cleared the error. Instead, I added a "forcibleerror" rule and a javascript method to set the message.
Usage:
To put it back in a valid state:
You can also do:
With this function in your code:
Then can set the error