fluentvalidation

Selective validation of child properties - Fluent Validation in MVC

拟墨画扇 提交于 2019-12-04 14:03:29
问题 I'm using Fluent Validation with the Ninject.Web.Mvc.FluentValidation library to automatically wire up all of my validators (and use dependency injection to create the validators). I created the following Models: public class Parent { public string Name { get; set; } public Child Child1 { get; set; } public Child Child2 { get; set; } } public class Child { public string ChildProperty { get; set; } } With the following validators: public class ParentValidator : AbstractValidator<Parent> {

What is the correct way to register FluentValidation with SimpleInjector?

拟墨画扇 提交于 2019-12-04 11:48:22
问题 I am able to register FluentValidation AbstractValidators using a FluentValidatorFactory . However, it doesn't feel right, because not all of the IoC container registrations happen during bootstrap / composition root. Instead, the fluent validators are registered by a separate factory: The composition root : public class SimpleDependencyInjector : IServiceProvider { public readonly Container Container; public SimpleDependencyInjector() { Container = Bootstrap(); } internal Container Bootstrap

How to perform async ModelState validation with FluentValidation in Web API?

情到浓时终转凉″ 提交于 2019-12-04 09:07:47
I setup a web api project to use FluentValidation using the webapi integration package for FluentValidation. Then I created a validator that uses CustomAsync(...) to run queries against the database. The issue is that the validation seems to deadlock when awaiting for the database task. I did some investigation, it seems that the MVC ModelState API is synchronous, and it calls a synchronous Validate(...) method that makes FluentValidation to call task.Result , causing the deadlock. Is it correct to assume that async calls won't work well with webapi integrated validation? And if that is the

Unobtrusive client validation data attributes are not rendered for nested property rules

浪尽此生 提交于 2019-12-04 08:24:02
Using FluentValidation 4.4, the following rules emit the correct unobtrusive validation data attributes on input fields: RuleFor(e => e.PrimaryContact).NotEmpty(); rendering the following html: <input class="text-box single-line k-textbox input-validation-error" data-val="true" data-val-required="'Primary Contact' should not be empty." id="PrimaryContact" name="PrimaryContact" type="text" value=""> However, a rule with a nested property does not emit any validation data attributes: RuleFor(e => e.Company.Name).NotEmpty(); rendering the following html: <input class="text-box single-line k

Override default ASP.NET MVC message in FluentValidation

末鹿安然 提交于 2019-12-04 05:17:58
I get the validation message "The value xxx is not valid for yyy". It happens when I post incorrect value for double type. I have no idea how to change it. Unfortunately this isn't something that FluentValidation has the ability to override - the extensibility model for MVC's validation is somewhat limited in many places, and I've not been able to find a way to override this particular message. An alternative approach you could use is to define two properties on your view model - one as a string, and one as a nullable double. You would use the string property for MVC binding purposes, and the

Passing the ErrorMessage for clientside validation

别来无恙 提交于 2019-12-03 20:57:27
Since there is no way to validate a property (with unobtrusive clientside validation) using multiple regex patterns (because validation type has to be unique) i decided to extend FluentValidation so i can do the following. RuleFor(x => x.Name).NotEmpty().WithMessage("Name is required") .Length(3, 20).WithMessage("Name must contain between 3 and 20 characters") .Match(@"^[A-Z]").WithMessage("Name has to start with an uppercase letter") .Match(@"^[a-zA-Z0-9_\-\.]*$").WithMessage("Name can only contain: a-z 0-9 _ - .") .Match(@"[a-z0-9]$").WithMessage("Name has to end with a lowercase letter or

Autofac - how to resolve Func for ISomething from Singleton where ISomething is InstancePerHttpRequest

这一生的挚爱 提交于 2019-12-03 18:55:13
问题 I'm trying to use Autofac to inject dependencies into FluentValidation in an MVC 4 app. I think I've got the strategy worked out, but I'm getting stuck with resolving my per-request ISomething from a singleton. Here's the scenario: I've got a validator that derives from FluentValidation's AbstractValidator. I've read that FluentValidation validators perform best as singletons, so my constructor expects a Func and stores that Factory for use later. When the validator is used, it should ask the

FluentValidation Call RuleSet and Common Rules

余生长醉 提交于 2019-12-03 17:33:28
问题 I have the following class public class ValidProjectHeader : AbstractValidator<Projects.ProjectHeader> { public ValidProjectHeader() { RuleFor(x => x.LobId).Must(ValidateLOBIDExists); RuleFor(x => x.CreatedByUserId).NotEmpty(); RuleFor(x => x.ProjectManagerId).NotEmpty(); RuleFor(x => x.ProjectName).NotEmpty(); RuleFor(x => x.SalesRepId).NotEmpty(); RuleFor(x => x.DeliveryDate).NotEmpty(); RuleFor(x => x.ProjectStatusId).NotEmpty(); RuleFor(x => x.DeptartmentId).NotEmpty(); RuleFor(x => x

SimpleInjector and FluentValidationFactory

元气小坏坏 提交于 2019-12-03 16:23:48
I am trying to automate the validation of my view models, I know I can just add a an attribute to specify my validation but there is an option to set up a factory to automate all that, I looked at: this answer and came up with this using simple injector 3.1: public class CustomValidatorFactory:ValidatorFactoryBase { private readonly Container siContainer; public CustomValidatorFactory(Container siContainer) { var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList(); this.siContainer = siContainer; this.siContainer.Register(typeof(IValidator<>), assemblies); } public override

FluentValidation Autofac ValidatorFactory

跟風遠走 提交于 2019-12-03 13:55:06
I need to be able to provide the IComponentContext to my ValidatorFactory to resolve FluentValidation Validators. I am a little stuck. ValidatorFactory public class ValidatorFactory : ValidatorFactoryBase { private readonly IComponentContext context; public ValidatorFactory(IComponentContext context) { this.context = context; } public override IValidator CreateInstance(Type validatorType) { return context.Resolve(validatorType) as IValidator; } } How do I provide the context and register the ValidatorFactory FluentValidation.Mvc.FluentValidationModelValidatorProvider.Configure(x => x