Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 一个Fluent验证规则中的条件和非条件_C#_Asp.net Mvc_Fluentvalidation - Fatal编程技术网

C# 一个Fluent验证规则中的条件和非条件

C# 一个Fluent验证规则中的条件和非条件,c#,asp.net-mvc,fluentvalidation,C#,Asp.net Mvc,Fluentvalidation,我需要验证邮政编码, -它必须始终有一个值(无条件) 如果在美国,则值应与正则表达式(有条件)匹配 我的目标是将一般规则和美国规则结合在一起 ,我试过: // doesn't check general rule for non-us RuleFor( m => m.Zip ) .NotEmpty() .WithMessage( "Zip code is required." ) .Matches( ZipCodeRegEx ) .When(

我需要验证邮政编码, -它必须始终有一个值(无条件)

如果在美国,则值应与正则表达式(有条件)匹配

我的目标是将一般规则和美国规则结合在一起 ,我试过:

// doesn't check general rule for non-us
RuleFor( m => m.Zip )
    .NotEmpty()
        .WithMessage( "Zip code is required." )
    .Matches( ZipCodeRegEx )
        .When( m => IsUnitedStates( m.CountryCode ) )
        .WithMessage( "Invalid Zip Code" );

// doesn't check general rule for non-us
RuleFor( m => m.Zip )
    .NotEmpty()
        .When( m => true )// always
        .WithMessage( "Zip code is required." )
    .Matches( ZipCodeRegEx )
        .When( m => IsUnitedStates( m.CountryCode ) )
        .WithMessage( "Invalid Zip Code" );

我怎样才能在一条规则中写下它?

不可能在一条规则中完成。FluentValidation不是为支持这种情况而设计的。

Zip是什么数据类型?一根绳子?您有什么ValidatorOptions.CascadeMode设置为?Zip是字符串,CascadeMode是Continue。您可以发布您的IsUnitedStates方法吗?private bool IsUnitedStates(string countryCode){return countryCode==“US”}另外,
ZipCodeRegEx
也可以使用一条规则来完成。在这里检查这个答案:在测试了你的链接和其他帖子中的所有答案后,我得出结论,这是不可能的。
// United States Rule:
RuleFor( m => m.Zip )
    .Matches( ZipCodeRegEx )
    .WithMessage( "Invalid Zip Code" )
    .When( m => IsUnitedStates( m.CountryCode ) );
// doesn't check general rule for non-us
RuleFor( m => m.Zip )
    .NotEmpty()
        .WithMessage( "Zip code is required." )
    .Matches( ZipCodeRegEx )
        .When( m => IsUnitedStates( m.CountryCode ) )
        .WithMessage( "Invalid Zip Code" );

// doesn't check general rule for non-us
RuleFor( m => m.Zip )
    .NotEmpty()
        .When( m => true )// always
        .WithMessage( "Zip code is required." )
    .Matches( ZipCodeRegEx )
        .When( m => IsUnitedStates( m.CountryCode ) )
        .WithMessage( "Invalid Zip Code" );