Search code examples
c#asp.net-mvcasp.net-core.net-corefluentvalidation

FluentValidation.NET equivalent to [Display(Name)]


Before FluentValidation.NET I could give a custom label to a properly like so:

[Display(Name="Blah")]
public string BlahBlahBlah { get; set; }

And I could consume this in several ways:

@Html.LabelFor(m => m.BlahBlahBlah)
@Html.DisplayNameFor(m => m.BlahBlahBlah)

<label asp-for="BlahBlahBlah"></label>

Now I want to remove all data annotations from my models, and move to fluent validation. In my validator, I have this:

RuleFor(o => o.BlahBlahBlah)
    .NotEmpty()
    .WithName("Blah");

But this does not work. Why?


Solution

  • WithName method in FluentValidation is used ONLY to tune validation error message if you want to replace C# property name to smth more user friendly (see Overriding the Default Property Name for details).

    So the answer is - you cannot replace Display with WithName() in general, only for validation error message.