MVC 4 ignores DefaultModelBinder.ResourceClassKey
Asked Answered
G

1

23

Adding a resource file to App_GlobalResources with a PropertyValueRequired key and changing DefaultModelBinder.ResourceClassKey to the file name has no effect on MVC 4. The string The {0} field is required is never changed. I don't want to set the resource class type and key on every required field. Am I missing something?

Edit:

I've made a small modification on Darin Dimitrov's code to keep Required customizations working:

public class MyRequiredAttributeAdapter : RequiredAttributeAdapter
{
    public MyRequiredAttributeAdapter(
        ModelMetadata metadata,
        ControllerContext context,
        RequiredAttribute attribute
    )
        : base(metadata, context, attribute)
    {
        if (attribute.ErrorMessageResourceType == null)
        {
            attribute.ErrorMessageResourceType = typeof(Messages);
        }
        if (attribute.ErrorMessageResourceName == null)
        {
            attribute.ErrorMessageResourceName = "PropertyValueRequired";
        }
    }
}
Gamine answered 22/9, 2012 at 15:38 Comment(0)
M
41

This is not specific to ASP.NET MVC 4. It was the same in ASP.NET MVC 3. You cannot set the required message using DefaultModelBinder.ResourceClassKey, only the PropertyValueInvalid.

One way to achieve what you are looking for is to define a custom RequiredAttributeAdapter:

public class MyRequiredAttributeAdapter : RequiredAttributeAdapter
{
    public MyRequiredAttributeAdapter(
        ModelMetadata metadata,
        ControllerContext context,
        RequiredAttribute attribute
    ) : base(metadata, context, attribute)
    {
        attribute.ErrorMessageResourceType = typeof(Messages);
        attribute.ErrorMessageResourceName = "PropertyValueRequired";
    }
}

that you will register in Application_Start:

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(RequiredAttribute),
    typeof(MyRequiredAttributeAdapter)
);

Now when a non-nullable field is not assigned a value, the error message will come from Messages.PropertyValueRequired where Messages.resx must be defined inside App_GlobalResources.

Maurinemaurise answered 22/9, 2012 at 17:17 Comment(6)
Perfect! I did a lot of research and found nothing similar. Thank you very much.Gamine
Is it possible to do something similar for the type validation, e.g. for dates?Sizeable
We researched why you need an adapter for this: the entry GlobalResources.PropertyValueRequired is only used for non-nullable types which are not marked as "Required". The "real" Required message can only be changed using this adapter solution.Versed
There exist DataTypeAttributeAdapter and CompareAttributeAdapter but unlike RequiredAttributeAdapter they are internal. What the hell?Shed
Great answer! I will add that resx file doesn't have to be located in App_GlobalResources folder. It doesn't have to be located in the same project at all.Oppugn
The derived RequiredAttributeAdapter does not seem to be called on custom datatypes, thus the metadata in the EditorTemplate does only contain the default error message. Does anyone know a solution for this?Introvert

© 2022 - 2024 — McMap. All rights reserved.