0

In ASP.NET MVC 5, value types on models have an implicit [Required] attribute. I do want this feature, but I want to tweak that attribute, i.e. implicitly apply a different attribute.

So how can I change which attribute is implicitly applied to value types (if at all)?

As far as I know, I could do this:

protected void Application_Start()
{
    // System.Web.Mvc.
    DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
}

Then go to each of my models and add a different [Required] attribute:

public class MyViewModel
{
    [MyRequired]
    public int Amount { get; set; }
}

Attribute just overrides the default error message now, may want to differ greatly in the future.

[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
public class MyRequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
{
    public MyRequiredAttribute()
    {
        if (ErrorMessage == null)
        {
            ErrorMessage = "{0} is required.";
        }
    }
}

Is there a better approach?


Potentially useful: System.Web.Mvc.DataAnnotationsModelValidatorProvider.cs

Sinjai
  • 1,085
  • 1
  • 15
  • 34
  • What do you want `[MyRequired]` to do? And why do you think its a _nightmarish approach_? –  Feb 16 '18 at 00:30
  • @Stephen *Nightmarish* may be a bit extreme, but I'm inclined not to like it because it requires refactoring a lot of code and everyone will have to remember that the default behavior has been changed. – Sinjai Feb 16 '18 at 00:42
  • But what are you trying to achieve here? What is your `MyRequiredAttribute` and what does it do and how is it different from the standard `RequiredAttribute` ? –  Feb 16 '18 at 00:44
  • @Stephen Sorry, I was getting to that. I was posting separate comments for clarity (long comments = hell, for my brain). At the moment, I'm just trying to change the error message, without flubbing around with [resource files](https://stackoverflow.com/questions/3040264/asp-net-mvc-2-changing-the-propertyvaluerequired-string/5207912#5207912) -- though maybe I should? – Sinjai Feb 16 '18 at 00:48
  • public class MyRequired: ValidationAttribute, IClientModelValidator { .. your logic ..} – Janaka Dissanayake Feb 16 '18 at 00:49
  • You should edit your question to show the `MyRequiredAttribute` (I think you might be misunderstanding how this all works) –  Feb 16 '18 at 00:49
  • You do have a few misunderstandings :). First, when you set `AddImplicitRequiredAttributeForValueTypes` to `false`, it just means that internally, a `[Required]` attribute is not added by default (and therefore it will not generate the `data-val-required` attribute if a `[Required]` attribute is not added explicitly. [This answer](https://stackoverflow.com/questions/48714556/customized-dataannotationsmodelmetadataprovider-not-working/48718239#48718239) might help explain that part) –  Feb 16 '18 at 01:07
  • @Stephen what I definitely don't understand is how the implicit required thing for value types even works. The source looks like it applies the bog standard [RequiredAttribute](https://github.com/dotnet/corefx/blob/master/src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/RequiredAttribute.cs), but as far as I can tell any value type should always pass `RequiredAttribute.IsValid()`... – Sinjai Feb 16 '18 at 01:08
  • @Stephen, yeah I get what `AddImplicitRequiredAttributeForValueTypes` does, it's right there in the source I linked in my question. – Sinjai Feb 16 '18 at 01:09
  • Second, your attribute makes no sense, so its not really clear what your wanting to achieve. My best guess is that you just want to override the default error message generate by the `Required` attribute? (in which case, that is not the way to do it) –  Feb 16 '18 at 01:10
  • Your assumption is correct about overriding the default error message. That probably isn't *the* way to do it, but it is *a* way, as in the code works. – Sinjai Feb 16 '18 at 01:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165257/discussion-between-stephen-muecke-and-sinjai). –  Feb 16 '18 at 01:11

0 Answers0