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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With