【问题标题】:Ignore case sensitive in FluentValidation在 FluentValidation 中忽略大小写敏感
【发布时间】:2017-07-21 16:32:30
【问题描述】:

任何人都有任何想法可以忽略 FluentValidation 中的大小写

示例 RuleFor(x => x.Gender).Ignorecase(); 这样用户就可以输入F 或f。

【问题讨论】:

    标签: c# .net fluentvalidation


    【解决方案1】:

    你能做到以下几点吗?

    RuleFor(x.ToLower() => x.Gender.ToLower());
    

    【讨论】:

      【解决方案2】:

      我认为内置的正则表达式验证可以完成工作: https://github.com/JeremySkinner/FluentValidation/wiki/c.-Built-In-Validators#regular-expression-validator

      有点像:

      RuleFor(x => x.Gender).Matches("^[FfMm]{1}$");
      

      【讨论】:

        【解决方案3】:

        可以通过创建扩展方法来实现

        public static bool EqualsIgnoreCase(this string from, string to)
         { 
           return string.Equals(from, to, StringComparison.OrdinalIgnoreCase); 
         }
        

        然后像下面这样使用它:

        RuleFor(x => x.Gender.EqualsIgnoreCase("f"))
        

        【讨论】:

          【解决方案4】:

          Equal Validator 支持这个:

          RuleFor(x => x.Gender).Equal("f", StringComparer.OrdinalIgnoreCase);
          

          【讨论】:

            猜你喜欢
            • 2016-11-18
            • 2011-06-11
            • 1970-01-01
            • 1970-01-01
            • 2012-03-04
            • 2016-02-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多