Hi,
I'm using this excellent validation in an MVC application. I have two model classes "Translation" and "TranslationCategory". They both have a custom validator class "TranslationValidator" and "TranslationCategoryValidator"
' Translation Class<FluentValidation.Attributes.Validator(GetType(Validation.TranslationValidator))> PublicClass Translation Inherits ModelBase Implements ITranslate PublicProperty Reference AsStringPublicProperty Category As TranslationCategory PublicProperty Translation As TranslationSnippet Implements ITranslate.Translation PublicProperty Translations As List(Of TranslationSnippet) Implements ITranslate.Translations EndClass' Translation Category<FluentValidation.Attributes.Validator(GetType(Validation.TranslationCategoryValidator))> PublicClass TranslationCategory Inherits ModelBase PublicProperty Name AsStringEndClass
Below are the validator classes
' Translation ValidatorPublicClass TranslationValidator Inherits AbstractValidator(Of Translation) PublicSubNew() RuleFor(Function(r) r.Reference).NotEmpty().WithMessage("Reference cannot be empty.") RuleFor(Function(r) r.Reference).Must(Function(model, reference) Not TranslationService.TranslationExists(model)).WithMessage("This translation already exists.") EndSubEndClass'TranslationCategory ValidatorPublicClass TranslationCategoryValidator Inherits AbstractValidator(Of TranslationCategory) PublicSubNew() RuleFor(Function(r) r.Name).NotEmpty().WithMessage("Name cannot be empty.") RuleFor(Function(r) r.Name).Must(Function(model, name) TranslationService.TranslationCategoryExists(model)).WithMessage("This category already exists.")EndSubEndClass
Now the problem is, that when I save a new Translation, the validations/validators for the property "Category" automatically get triggered. Is this normal behavior? Because when I read the
documentation, validators for child properties/complex properties should explicitly be added to the parent validator, which is not my case.
Thanks for your reply
David