We are trying to create some custom validation with MVC 4 data annotations, the validation we are creating is kind of message prompts more than restrictive validation. First of all we have created some custom validation classes inheriting from the ValidationAttribute Class and overriding the IsValid() method to test the data and return an ValidationResult if not valid. The view that displays this data has Partial views that use EditorTemplates to display razor generated data using our custom data annotations and many built in validation, all this is wrapped in a form like this
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
Our requirements are to let the user post back the data and partially save the forms but prompt them on any invalid fields so are using the CSS class on the form submit to allow postback like so
<input type="submit" value="Save" class="cancel"/>
All this is working fine but we have now requirements to display all the error messages on page load which I did not see to be a problem until I tried it…
I found a few examples that used jquery in the $(document).ready event that called the form valid methods as seen here
Manual form validation in MVC 3 and JQuery
but this did not seem to work for us the $(‘form’).Validate() does not seem to do anything the only call that seems to fire the forms validation is $(‘form’).valid() But this only seems to show the built in validation such as the [Required] attributes and the only way to get the custom validation messages to show is to use the submit button to post back the form.
There must be a way to get my custom data annotations to display the messages without posting back the page the first time right? Any help would be greatly appreciated.