fluentvalidation

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

社会主义新天地 提交于 2019-12-06 03:46:00
问题 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

Fluent validation: set custom message on custom validation

空扰寡人 提交于 2019-12-05 18:09:44
问题 Scenario I have a custom rule to validate the shipping cost of an order: public class OrderValidator : BaseValidator<Order> { private string CustomInfo { get; set; } public OrderValidator() { //here I call the custom validation method and I try to add the CustomInfo string in the message RuleFor(order => order.ShippingCost).Cascade(CascadeMode.StopOnFirstFailure).NotNull().Must( (order, shippingCost) => CheckOrderShippingCost(order, shippingCost) ).WithMessage("{PropertyName} not set or not

FluentValidation checking for duplicate entity in a sub-collection

偶尔善良 提交于 2019-12-05 16:40:06
I have a MainEntity class and it has a collection of SubEntity . The following is the current validation: public class MainEntityValidator : AbstractValidator<MainEntity> { public MainEntityValidator() { RuleFor(x => x.SubEntities).SetCollectionValidator(new SubEntityValidator()); } public class SubEntityValidator : AbstractValidator<SubEntity> { public SubEntityValidator() { RuleFor(x => x.Field1).NotNull(); RuleFor(x => x.Field2).NotNull(); } } } How can I add a validation rule so that only unique SubEntity objects (based on Field1 and Field2 ) must be in the collection? If you need to apply

Should i create a new Type for Collection in FluentValidation?

假如想象 提交于 2019-12-05 13:14:41
I am trying to find if there is way available in FluentValidation which allows a collection to be validated for a Validator at Root Level. For e.g. As shown below, a validator is available for CustomerValidator for a class Customer . using FluentValidation; public class CustomerValidator: AbstractValidator<Customer> { public CustomerValidator() { RuleFor(customer => customer.Surname).NotEmpty(); RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Please specify a first name"); RuleFor(customer => customer.Discount).NotEqual(0).When(customer => customer.HasDiscount); RuleFor(customer

Fluent Validation doesn't validate the entire form the first time

放肆的年华 提交于 2019-12-05 07:33:56
So I'm using Fluent Validation on a form. When I click submit and have nothing entered, I get a validation error for Date of Birth. If I enter a DoB, then I get the validation for First Name. Why is this happening? I can't figure out what I wired up wrong. My form: @using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.HiddenFor(customer => customer.CustomerIncomeInfo.CustomerEmploymentInfoModel.EmployerModel.Id) <!-- basic customer info --> <fieldset> <legend>Customer Info</legend> @Html.ValidationSummary(false, "Please correct the errors and try again", new { @class = "text-danger" })

Passing the ErrorMessage for clientside validation

坚强是说给别人听的谎言 提交于 2019-12-05 04:08:17
问题 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

Using CascadeMode.StopOnFirstFailure on a validator level

时光怂恿深爱的人放手 提交于 2019-12-05 02:21:54
From the FluentValidation documentation I learned that I can abort validation by setting the cascade mode. RuleFor(x => x.Surname) .Cascade(CascadeMode.StopOnFirstFailure) .NotNull() .NotEqual("foo"); That way if the property Surname is null, the equality check won't be executed and a null pointer exception prevented. Further down in the documentation it is implied that this would also work not only within a rule but on a validator level as well. public class PersonValidator : AbstractValidator<Person> { public PersonValidator() { // First set the cascade mode CascadeMode = CascadeMode

FluentValidation for When & must?

会有一股神秘感。 提交于 2019-12-05 02:14:20
I am trying use FluentValidation validaton when dropdownlist value is yes and the field must be date. it is working when dropdownlist is yes checking for date . But also showing validation when I select No still it says Must be date . It should not validate anymore if dropdownlist value otherthan the yes . How can we do that? RuleFor(x => x.DtPublishedTimeText) .NotEmpty() .When(HasMaterialPublishedElseWhereText) .WithMessage("Required Field") .Must(BeAValidDate) .WithMessage("Must be date"); private bool BeAValidDate(string val) { DateTime date; return DateTime.TryParse(val, out date); }

WebAPi - unify error messages format from ApiController and OAuthAuthorizationServerProvider

可紊 提交于 2019-12-04 23:49:25
In my WebAPI project I'm using Owin.Security.OAuth to add JWT authentication. Inside GrantResourceOwnerCredentials of my OAuthProvider I'm setting errors using below line: context.SetError("invalid_grant", "Account locked."); this is returned to client as: { "error": "invalid_grant", "error_description": "Account locked." } after user gets authenticated and he tries to do "normal" request to one of my controllers he gets below response when model is invalid (using FluentValidation): { "message": "The request is invalid.", "modelState": { "client.Email": [ "Email is not valid." ], "client

Nested Razor layouts causing client validation to fail

 ̄綄美尐妖づ 提交于 2019-12-04 19:07:57
Here's my original create page (no nesting) - Client validation works @model TennisClub.ViewModels.ClubMember.EditorModel @{ ViewBag.Title = "New Club Member"; ViewBag.LegendTitle = "Club Member"; } <h2>@ViewBag.Title</h2> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm()) { @Html.ValidationSummary(true, "Errors were found on the form. Please correct all errors and try again.") <fieldset> <legend>@ViewBag.LegendTitle<