Child Model Validation using Parent Model Values. Fluent Validation. MVC4

Create a custom property validator like this public class AllChildBirtdaysMustBeLaterThanParent : PropertyValidator { public AllChildBirtdaysMustBeLaterThanParent() : base(“Property {PropertyName} contains children born before their parent!”) { } protected override bool IsValid(PropertyValidatorContext context) { var parent = context.ParentContext.InstanceToValidate as Parent; var list = context.PropertyValue as IList<Child>; if (list != null) { return ! (list.Any(c => parent.BirthDay > … Read more

unobtrusive client validation using fluentvalidation and asp.net mvc LessThanOrEqualTo not firing

Neither of the LessThanOrEqualTo or GreaterThanOrEqualTo rules are supported by client side validation as explained in the documentation. This means that if you want to have client side validation for them you will need to write a custom FluentValidationPropertyValidator and implement the GetClientValidationRules method which will allow you to register a custom adapter and implement … Read more

FluentValidation rule for null object

EDIT 2022-07-19 As some commenters have pointed out, please check out answer https://stackoverflow.com/a/52784357/1943 for a newer implementation. I haven’t personally vetted, but it’s worth a try to give that a go first. If you’re using an older version, or you enjoy nostalgia, my original answer below is from 2013. You should be able to override … Read more

FluentValidation: Check if one of two fields are empty

You can use When/Unless condition: RuleFor(m => m.FirstName).NotEmpty().When(m => string.IsNullOrEmpty(m.LastName)); RuleFor(m => m.LastName).NotEmpty().When(m => string.IsNullOrEmpty(m.FirstName)); or RuleFor(m => m.FirstName).NotEmpty().Unless(m => !string.IsNullOrEmpty(m.LastName)); RuleFor(m => m.LastName).NotEmpty().Unless(m => !string.IsNullOrEmpty(m.FirstName)); As for your second question, FluentValidation works with client-side validation, but not all rules are supported. Here you can find validators, that are supported on the client-side: NotNull/NotEmpty Matches … Read more

C# FluentValidation for a hierarchy of classes

public class Derived2Validator : AbstractValidator<Derived2> { public Derived2Validator() { Include(new BaseValidator()); Include(new Derived1Validator()); RuleFor(d => d.Derived1Name).NotNull(); } } Derived2Validator does not need to inherit BaseValidator or Derived1Validator. Use the Include method to include the rules from other validators.

ViewModel validation for a List

If you are using Data Annotations to perform validation you might need a custom attribute: public class EnsureOneElementAttribute : ValidationAttribute { public override bool IsValid(object value) { var list = value as IList; if (list != null) { return list.Count > 0; } return false; } } and then: [EnsureOneElement(ErrorMessage = “At least a person … Read more

Conditional Validation using Fluent Validation

Fluent validation supports conditional validation, just use the When clause to check the value of the secondary field: https://docs.fluentvalidation.net/en/latest/conditions.html Specifying a condition with When/Unless The When and Unless methods can be used to specify conditions that control when the rule should execute. For example, this rule on the CustomerDiscount property will only execute when IsPreferredCustomer … Read more

Fluent Validation vs. Data Annotations

I prefer Fluent Validation: It gives me far better control of my validation rules Doing conditional validation on different properties is so much easier compared to Data Annotations It separates the validation from my view models Unit testing is far easier compared to Data Annotations It has excellent client side validation support for most standard … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)