I have a custom model validator on a particular property. It shows the error at the top (in the Html.EnableClientValidation section). However, it does not show the message next to the field, even though I have a corresponding ValidationMessageFor call that
does successfully show the DataAnnotation violations associated with the property. Are messages returned through ModelValidator.Validate supposed to show buy the field like the DataAnnotation messages do?
If so, is it a problem in that my container actually has a list of these objects that i'm validating? (And somehow we are missing the prefix in ModelValidationResult.MemberName?) I had that theory, but hardcoding the correct name like this example did not
make it better:
return new[]{ new ModelValidationResult
{
MemberName = "Fields[3]." + meta.PropertyName,
Message = "Invalid Date Format or Range"
}};
When you're writing a validator which is validating the value it's given, then MemberName should be null or empty. When you're writing a validator which is validating a sub-property of the value it's given, then MemberName should be the name of the property.
Don't worry about prefixes, those are taken care of by the system for you.
It's hard to tell without seeing more code, but I would try either not setting MemberName, or setting it only to meta.PropertyName.
Marked as answer by ricka6 on Feb 05, 2010 07:19 PM
BranTheMan
None
0 Points
17 Posts
ModelValidationResult.Message should show at top and by field?
Feb 03, 2010 10:33 PM|LINK
I have a custom model validator on a particular property. It shows the error at the top (in the Html.EnableClientValidation section). However, it does not show the message next to the field, even though I have a corresponding ValidationMessageFor call that does successfully show the DataAnnotation violations associated with the property. Are messages returned through ModelValidator.Validate supposed to show buy the field like the DataAnnotation messages do?
If so, is it a problem in that my container actually has a list of these objects that i'm validating? (And somehow we are missing the prefix in ModelValidationResult.MemberName?) I had that theory, but hardcoding the correct name like this example did not make it better:
return new[]{ new ModelValidationResult
{
MemberName = "Fields[3]." + meta.PropertyName,
Message = "Invalid Date Format or Range"
}};
ModelValidationResult EnableClientValidation ModelValidator
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: ModelValidationResult.Message should show at top and by field?
Feb 04, 2010 02:13 AM|LINK
I'm guessing the names don't line up correctly.
When you're writing a validator which is validating the value it's given, then MemberName should be null or empty. When you're writing a validator which is validating a sub-property of the value it's given, then MemberName should be the name of the property. Don't worry about prefixes, those are taken care of by the system for you.
It's hard to tell without seeing more code, but I would try either not setting MemberName, or setting it only to meta.PropertyName.
BranTheMan
None
0 Points
17 Posts
Re: ModelValidationResult.Message should show at top and by field?
Feb 04, 2010 10:30 PM|LINK
Leaving it blank does fix the problem! Who woulda thought...