Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating certain parts of input string using Fluent Validation

I am using ASP.NET 4 and Fluent Validation.

I am trying to setup a rule that checks if my user name starts with "adm".

I have the following, giving me errors. I tried to follow the online sample but it is not working:

RuleFor(x => x.UserName)
     .NotNull()
     .WithMessage("Required")
     .Must(x => x.UserName.StartsWith("adm"))
     .WithMessage("Must start with ADM");

I don't think I am doing it correctly?

I've event tried:

.Must(x => x.UserName.Substring(0, 3) == "adm")

Error for both:

Delegate 'System.Func<MyProject.ViewModels.LogOnViewModel,string,bool>' does not take 1 arguments
like image 659
Brendan Vogt Avatar asked Dec 15 '22 12:12

Brendan Vogt


2 Answers

Try .Must(str => str.StartsWith("adm"))

like image 171
Vladimir Avatar answered Dec 18 '22 02:12

Vladimir


RuleFor(f => f.UserName).NotNull();
RuleFor(f => f.UserName).Must((f, t) => f.UserName.StartsWith("adm")).WithMessage("Must start with ADM");

I solved this way.

like image 25
Erman Avatar answered Dec 18 '22 01:12

Erman