Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FluentValidation.NET equivalent to [Display(Name)]

Tags:

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?

like image 876
Matthew Layton Avatar asked Dec 27 '16 20:12

Matthew Layton


1 Answers

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.

like image 196
Oleksandr Kobylianskyi Avatar answered Sep 23 '22 16:09

Oleksandr Kobylianskyi