-1
我從來沒有聽說過FluentValidation的直到今天當我工作的一個項目,所以我遇到了一個問題。我有這個..如何使用FluentValidation將其他字段爲空的驗證應用於一個字段?
RuleFor(x=>x.Company)
.NotEmpty()
.WithMessage("Company Required");
RuleFor(x => x.FirstName)
.NotEmpty()
.WithMessage(FirstName Required");
RuleFor(x => x.LastName)
.NotEmpty()
.WithMessage("LastName Required");
和一羣其他RuleFor statements.What我需要做的是..
如果公司字段爲空,則需要對名字和姓氏的驗證,但如果公司字段不爲空,則不適驗證,名字和姓氏
我不知道從哪裏開始。
編輯
我試過當條件以及與此
When(x => x.Company == "" || x.Company == null,() =>
{
RuleFor(x => x.FirstName)
.NotEmpty()
.WithMessage("FirstName Required");
RuleFor(x => x.LastName)
.NotEmpty()
.WithMessage("LastName Required");
});
和我「認爲」應該都掀起了名字和姓氏的驗證來了,但事實並非如此。
然後我試圖這樣
When(x.Company.length == 0,() =>
{
RuleFor(x => x.FirstName)
.NotEmpty()
.WithMessage("FirstName Required");
RuleFor(x => x.LastName)
.NotEmpty()
.WithMessage("LastName Required");
});
,同樣的事情發生了,名字和姓氏的驗證沒有發生。
[指定與當/除非條件](https://github.com/JeremySkinner/FluentValidation/wiki/d.-Configuring-a-Validator#user-content-specifying-a-condition-with -whenunless) – jmoerdyk
@jmoerdyk,我在看,自己目前 – Chris
我的困惑是在鋪設的時候,因爲例子顯示它如何與一個布爾值,而不是一個字符串 – Chris