Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fluent validation with when on client side

In a ASP.NET MVC4 application we are using FluentValidation for validating our models. In certain cases we only want to validate a property when another property has a value. We use the When keyword to accomplish this. A simple validation class looks like this:

public class PersonValidator : AbstractValidator<Person>
{
    public PersonValidator()
    {
        RuleFor(item => item.FirstName).NotEmpty();
        RuleFor(item => item.LastName).NotEmpty().When(item => !string.IsNullOrEmpty(item.FirstName))
    }
}

We would like to have client side validation for this. I tried to create a custom FluentValidationPropertyValidator. But I can't find a way to pickup the When part of the validation rule. Can someone point me in right direction?

like image 773
pieter_dv Avatar asked Apr 11 '26 00:04

pieter_dv


1 Answers

FluentValidation does now support client-side validation. The following validators are supported on the client:

  • NotNull/NotEmpty
  • Matches (regex)
  • InclusiveBetween (range)
  • CreditCard
  • Email
  • EqualTo (cross-property equality comparison)
  • MaxLength
  • MinLength
  • Length

https://fluentvalidation.net/aspnet

like image 108
Rebecca Avatar answered Apr 13 '26 23:04

Rebecca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!