Say I have a custom validation attribute ValidateFooIsCompatibleWith
model like so:
public class FooPart
{
public string Foo { get; set; }
public string Eey { get; set; }
}
public class FooableViewModel
{
public FooPart Foo1 { get; set; }
[ValidateFooIsCompatibleWith("Foo1")]
public FooPart Foo2 { get; set; }
}
Let's say I also have custom EditorTemplates defined for FooPart
:
@Html.TextBoxFor(m => m.Foo)
@Html.TextBoxFor(m => m.Eey)
And thus my view is essentially:
@Html.EditorFor(m => m.Foo1)
@Html.EditorFor(m => m.Foo2)
Server side, the validation works fine. However, no matter what I try, I can't get the rendered html to add the rule.
If I implement IClientValidatable
, it turns out that GetClientValidationRules()
never gets called. (I have successfully used IClientValidatable
with "simple" fields before).
I also tried registering my own custom adapter by inheriting from DataAnnotationsModelValidator<TAttribute>
and registering it in the global.asax
with DataAnnotationsModelValidatorProvider.RegisterAdapter(...)
That approach too fails to call GetClientValidationRules()
.
** Update **
If a add both a custom ModelMetadataProvider
and a custom ModelValidatorProvider
so that I can set breakpoints, I notice an interesting bit of behavior:
- a request is made to the
ModelMetadataProvider
for metadata with aContainerType
ofFooableViewModel
and aModelType
ofFooPart
. However, no corresponding request is made to theModelValidatorProvider
, so I can't insert my custom client validation rules there. - requests are made to the
ModelValidatorProvider
with aContainerType
ofFooPart
and aModelType
ofstring
for both theFoo
andEey
properties. But at this level, I don't know the attributes applied to theFooPart
property.
How can I get the MVC framework to register my custom client validation rules for complex types?