[Required(ErrorMessage="Applicant's First Name is required.")]
[StringLength(25,ErrorMessage ="Maximum length is 25 characters for Applicant's First Name")]
public string CustomerName{ get; set; }
In my view I enabled client-side validation.
<script type="text/javascript">
//<![CDATA[
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"CustomerName","ReplaceValidationMessageContents":false,"ValidationMessageId":"CustomerName_validationMessage","ValidationRules":[{"ErrorMessage":"Applicant\u0027s First Name is required","ValidationParameters":{},"ValidationType":"required"},{"ErrorMessage":"Maximum length is 25 characters for Applicant\u0027s First Name","ValidationParameters":{"minimumLength":0,"maximumLength":25},"ValidationType":"stringLength"}]}],"FormId":"form0","ReplaceValidationSummary":false});
//]]>
</script>
So, is this the way it's supposed to work?
Shouldn't enabling client-side validation highlight non-validating elements by default, without an needing to create an associated Html.ValidationMessageFor(), if it has a DataAnnotation validation attribute?
public class CustomerViewData
{
public Customer customer{ get; set;}
}
And this is the Customer class:
public class Customer
{
[Required(ErrorMessage = Customer Name is required.")]
public string CustomerName{ get; set; }
[Required(ErrorMessage = Customer Address is required.")]
public string CustomerAddress{ get; set; }
[Required(ErrorMessage = Customer Phone is required.")]
public string CustomerPhone{ get; set; }
}
What should the modelName parameter for Validate() be?
What should the modelName parameter for Validate() be?
Pass the same thing you would have passed to ValidationMessage() or ValidationMessageFor(). So <%= Html.ValidatrFor(model=>model.customer.CustomerName) %>, etc.
Marked as answer by ricka6 on Feb 12, 2010 01:13 AM
Is there one method that I can call to automatically wire up all the client-side validation metadata for all of the properties in my view model?
No, because we don't know which fields you had intended the user to edit (or even wanted validation to apply to) and thus should output validation for.
Marked as answer by ozmosis on Feb 11, 2010 11:55 PM
I was thinking that if the model-bound element is an input of type text, radio, checkbox, or textarea, etc, that is not hidden or disabled, that a method could wire it all up to make things quick for views with a lot of model-bound elements.
But sure, the weight of the exceptions and assumptions of that case are heavier than explicitly having to set each element to validate.
Thanks Rick, yes that works, but I need to be able to arrange the elements on the page. It's cool though, I'm using ValidateFor() to get what I want. I was just wondering if there was a snappy, one-method way to do it. : )
ozmosis
Member
8 Points
23 Posts
MVC 2 RC2 Clientside validation: Html.ValidationMessageFor() required even if using DataAnnotatio...
Feb 10, 2010 05:55 PM|LINK
I'm using DataAnnotations in my viewmodel class:
[Required(ErrorMessage="Applicant's First Name is required.")] [StringLength(25,ErrorMessage ="Maximum length is 25 characters for Applicant's First Name")] public string CustomerName{ get; set; }In my view I enabled client-side validation.
<script type="text/javascript"> //<![CDATA[ if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; } window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"CustomerName","ReplaceValidationMessageContents":false,"ValidationMessageId":"CustomerName_validationMessage","ValidationRules":[{"ErrorMessage":"Applicant\u0027s First Name is required","ValidationParameters":{},"ValidationType":"required"},{"ErrorMessage":"Maximum length is 25 characters for Applicant\u0027s First Name","ValidationParameters":{"minimumLength":0,"maximumLength":25},"ValidationType":"stringLength"}]}],"FormId":"form0","ReplaceValidationSummary":false}); //]]> </script>So, is this the way it's supposed to work?
Shouldn't enabling client-side validation highlight non-validating elements by default, without an needing to create an associated Html.ValidationMessageFor(), if it has a DataAnnotation validation attribute?
Thanks again!
MVC - 2 RC2
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: MVC 2 RC2 Clientside validation: Html.ValidationMessageFor() required even if using DataAnnot...
Feb 11, 2010 12:46 AM|LINK
If you want client validation, but not a message (i.e., you'd prefer the message to show up in the summary only), then call Html.Validate.
ozmosis
Member
8 Points
23 Posts
Re: MVC 2 RC2 Clientside validation: Html.ValidationMessageFor() required even if using DataAnnot...
Feb 11, 2010 01:37 PM|LINK
Nice. Would you please provide me an example of what to pass as the modelName parameter?
I've tried putting every permutation possible, but it doesn't seem to be retrieving the metadata from my model into the page script.
Say I have this form:
<% Html.EnableClientValidation(); %> <% using (Html.BeginForm("SaveCustomer", "Customer", FormMethod.Post)) { %> <%= Html.TextBoxFor(model=>model.customer.CustomerName)%> <%= Html.TextBoxFor(model=>model.customer.CustomerAddress)%> <%= Html.TextBoxFor(model=>model.customer.CustomerPhone)%> <%= Html.SubmitButton("submitButton", "Next")%> <% Html.Validate("what_should_be here?"); %> <% } %>And this is my page directive:
public class CustomerViewData { public Customer customer{ get; set;} }And this is the Customer class:
public class Customer { [Required(ErrorMessage = Customer Name is required.")] public string CustomerName{ get; set; } [Required(ErrorMessage = Customer Address is required.")] public string CustomerAddress{ get; set; } [Required(ErrorMessage = Customer Phone is required.")] public string CustomerPhone{ get; set; } }What should the modelName parameter for Validate() be?
levib
Star
7702 Points
1099 Posts
Microsoft
Re: MVC 2 RC2 Clientside validation: Html.ValidationMessageFor() required even if using DataAnnot...
Feb 11, 2010 04:52 PM|LINK
Pass the same thing you would have passed to ValidationMessage() or ValidationMessageFor(). So <%= Html.ValidatrFor(model=>model.customer.CustomerName) %>, etc.
ozmosis
Member
8 Points
23 Posts
Re: MVC 2 RC2 Clientside validation: Html.ValidationMessageFor() required even if using DataAnnot...
Feb 11, 2010 06:59 PM|LINK
Thanks Levi.
I have a view model that has lots of properties, each one with its own DataAnnotation attributes for validation.
Is there one method that I can call to automatically wire up all the client-side validation metadata for all of the properties in my view model?
This works for Lists in the view model class:
<% Html.Validate("Model"); %>P.S. The method description in the intellisense tooltip for Html.Validate is misleading because it says:
"Retrieves the validation metadata for the specified model and applies each rule to the data field."
but I think it would be clearer if it read:
"Retrieves the validation metadata for the specified model property and applies each rule to that data field."
I'm really digging everything so far. Way to go. :)
levib
Star
7702 Points
1099 Posts
Microsoft
Re: MVC 2 RC2 Clientside validation: Html.ValidationMessageFor() required even if using DataAnnot...
Feb 11, 2010 09:18 PM|LINK
No, because we don't know which fields you had intended the user to edit (or even wanted validation to apply to) and thus should output validation for.
ozmosis
Member
8 Points
23 Posts
Re: MVC 2 RC2 Clientside validation: Html.ValidationMessageFor() required even if using DataAnnot...
Feb 11, 2010 11:53 PM|LINK
I was thinking that if the model-bound element is an input of type text, radio, checkbox, or textarea, etc, that is not hidden or disabled, that a method could wire it all up to make things quick for views with a lot of model-bound elements.
But sure, the weight of the exceptions and assumptions of that case are heavier than explicitly having to set each element to validate.
Thanks again!
ricka6
All-Star
15088 Points
2277 Posts
Microsoft
Moderator
Re: MVC 2 RC2 Clientside validation: Html.ValidationMessageFor() required even if using DataAnnot...
Feb 12, 2010 01:16 AM|LINK
>>Is there one method that I can call to automatically wire up all the client-side validation metadata for all of the properties in my view model?
If you have the right viewModel, you can use
which wires up all the validators
ozmosis
Member
8 Points
23 Posts
Re: MVC 2 RC2 Clientside validation: Html.ValidationMessageFor() required even if using DataAnnot...
Feb 12, 2010 12:32 PM|LINK
Thanks Rick, yes that works, but I need to be able to arrange the elements on the page. It's cool though, I'm using ValidateFor() to get what I want. I was just wondering if there was a snappy, one-method way to do it. : )
ricka6
All-Star
15088 Points
2277 Posts
Microsoft
Moderator
Re: MVC 2 RC2 Clientside validation: Html.ValidationMessageFor() required even if using DataAnnot...
Feb 12, 2010 05:35 PM|LINK
You can create your own like I do in my blog entry DRY-ing out the MVC 2 Templated Helpers - It's a bit dated (for example, we now have ValidationFor())