fluentvalidation

Fluent Validation not working on length

心已入冬 提交于 2019-12-12 11:05:09
问题 I am trying to get Fluent Validation to work correctly on my client side validation. I am using ASP.NET MVC 3. I have a title that is required and it must be between 1 and 100 characters long. So while I am typing in the title an error message displays that is not in my ruleset. Here is my rule set: RuleFor(x => x.Title) .NotEmpty() .WithMessage("Title is required") .Length(1, 100) .WithMessage("Title must be less than or equal to 100 characters"); Here is the error message that is displayed:

FluentValidation rules chaining not stopping at first failure

蹲街弑〆低调 提交于 2019-12-12 10:46:58
问题 I have a model: public class DTO { public int[] StatementItems { get; set; } } Which I want to validate that: StatementItems is not null StatementItems is not empty StatementItems does not contain any duplicate IDs The validation rule chain I created is: RuleFor(x => x.StatementItems).NotNull().NotEmpty().Must(x => x.Distinct().Count() == x.Count()); And I have a test as: _validator.ShouldHaveValidationErrorFor(x => x.StatementItems, null as int[]); When I run the test passing in a null value

Using FluentValidation's WithMessage method with a list of named parameters

五迷三道 提交于 2019-12-12 07:44:28
问题 I am using FluentValidation and I want to format a message with some of the object's properties value. The problem is I have very little experience with expressions and delegates in C#. FluentValidation already provides a way to do this with format arguments. RuleFor(x => x.Name).NotEmpty() .WithMessage("The name {1} is not valid for Id {0}", x => x.Id, x => x.Name); I would like to do something like this to avoid having to change the message string if I change the order of the parameters.

controller input validation in mvc api

Deadly 提交于 2019-12-12 04:45:59
问题 i have built an api and need to validate input objects passed as parameters to controller actions. Sometimes the same input class will have differing compulsory properties for different actions. so public class test { public ActionResult Create(User user) { //here user_name must be present but no user_id } public ActionResult Delete(User user) { //here only user_id must be present. } } I have had a look at fluentValidation.net but it seems one has to create rules on a per class basis not

Unable to hit duplicate controller route after installing fluentvalidation.webapi?

独自空忆成欢 提交于 2019-12-12 02:35:16
问题 Recently i installed Fluent I have two projects - one MVC and other Web Api project. Both have same controller named as Deals controller. After fluent installation, i got problem that web api request is not able to locate any DealsController. In MVC's Gloabal asax- Application_Start method: protected void Application_Start(object sender, EventArgs e) { System.Web.Http.GlobalConfiguration.Configure(CompanyName.Service.WebApiConfig.Register); CompanyName.RouteConfig.RegisterRoutes(System.Web

NuGet Fails to Install FluentValidation

有些话、适合烂在心里 提交于 2019-12-11 19:27:08
问题 I am attempting to install the NuGet package Nancy.Validation.FluentValidation but the installation fails due to, I think, unsupported frameworks? The full NuGet error is below. I am using .NET 4.5. I cant see why it would fail? Any ideas what is going wrong? Output when installing Nancy.Validation.FluentValidation : Attempting to resolve dependency 'Nancy (≥ 1.2.0)'. Attempting to resolve dependency 'FluentValidation'. Installing 'FluentValidation 5.6.2.0'. Successfully installed

How to create warning messages in FluentValidation

本小妞迷上赌 提交于 2019-12-11 13:22:41
问题 I would like to add style to the validation messages based on enum type. FluentValidation offers possibility to add custom state for messages by using WithState method. Depending on which enum is used it would add a class for that message in HTML, so later I could add styling to it. Model validator class: public class SampleModelValidator : AbstractValidator<SampleModelValidator> { public SampleModelValidator() { RuleFor(o => o.Age)).NotEmpty() // Using custom state here .WithState(o =>

FluentValidation Validate checkbox and password on the client with EqualValidator

孤街醉人 提交于 2019-12-11 10:34:51
问题 I implemented the code below to have a way to validate a checkbox unobtrusively found this code posted by Darin Dimitrov. It works really well for the checkbox, but it does not work if you also have password and confirm password validated with the EqualValidator. I wonder if the custom Validator can be changed to take the checkbox and password validation into account. Or do I need to write a custom Validator for the password? Model [Validator(typeof(MyViewModelValidator))] public class

Generic Validator for Model T Using Fluent Validation?

本小妞迷上赌 提交于 2019-12-11 03:36:56
问题 I just got introduced to Fluent Validation yesterday and I think it's pretty cool. I have tried it and it works. But my application currently has several models and I must admit it is stressful to write Validators for each model. Is it possible to have it written in Generics and find a way to get each model validated with it? This is how my Validator is currently written. But I don't know how to write it in generics. EmployeeValidator.cs public class EmployeeValidator : AbstractValidator