开发者

Multiattribute custom validtors

开发者 https://www.devze.com 2022-12-14 12:20 出处:网络
I was looking at t开发者_StackOverflow社区his article on custom validators in aps.net mvc 2 and I was wondering how I might go about creating a more complex validation which worked on multiple fields

I was looking at t开发者_StackOverflow社区his article on custom validators in aps.net mvc 2 and I was wondering how I might go about creating a more complex validation which worked on multiple fields at once. Say something like

 if(fieldA > 7 and fieldB < 15)

The attribute method of creating validators doesn't seem like it would work for that.


I have started using Fleunt validation and is allows you to achieve quite complex validation with ease. While it takes some getting used to, I have found it very flexible. For example, here we make sure the length of a user password is between 7 and 15!

    public UserValidator() {
        RuleFor(user=> user.Password)
            .Length(7, 15)
            .WithMessage("Password to short/long");
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消