question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not all properties are validated server side on first submit

See original GitHub issue

System Details

  • FluentValidation version: 8.0.101
  • FluentValidation.Mvc5 version: 8.0.100
  • Web Framework version MVC 5: 5.2.6.0

Issue Description

I have very simple sample Model generated using Entity Framework 6 Database First approach that looks like this:

    [Validator(typeof(SupplierEntityValidator))]
    public partial class Supplier
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int CurrencyId { get; set; }
        public System.DateTime CreatedDate { get; set; }
        public System.DateTime ModifiedDate { get; set; }
    
        public virtual Currency Currency { get; set; }
    }

    public partial class Currency
    {
        public int Id { get; set; }
        public string CurrencyCode { get; set; }
        public decimal BaseQuantity { get; set; }
        public decimal ExchangeRate { get; set; }
    
        public virtual Supplier Supplier { get; set; }
    }

Id, Name, CurrencyId, CreatedDate, ModifiedDate are set to Not Null in DB which is mapped in the EF model.

3

Validator class for Supplier looks like this:

    public class SupplierEntityValidator : AbstractValidator<Supplier>
    {
        public SupplierEntityValidator()
        {
            RuleFor(x => x.CurrencyId)
                .NotEmpty()
                .WithMessage("Wybór waluty jest wymagany.");

            RuleFor(x => x.Name)
                .NotEmpty()
                .WithMessage("Nazwa jest wymagana.");
        }
    }

Client side validation is turned off in web.config as I would like to use the possibility to filter (include/exclude) validated properties of the model which is not possible with FluentValidation on the client side:

  <appSettings>
    <add key="ClientValidationEnabled" value="false" />
    <add key="UnobtrusiveJavaScriptEnabled" value="false" />
  </appSettings>

When I submit form without any values filled in I get only validation messages for fields CurrencyId, CreatedDate and ModifiedDate. 1 It seems as the validation message for CurrencyId field comes from the SupplierEntityValidator class, messages for CreatedDate and ModifiedDate seem to be coming from default EF generated model constraints. After filling them in and resubmitting the form the validation message for Name finally occurs coming from the SupplierEntityValidator. 2 Why Name validation message does not appear after the first submit?

When I run in SupplierController:

var validator = new SupplierEntityValidator();
var errors = validator.Validate(supplier);

it results in 2 errors during the first form submition.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Nov 29, 2018

I’m not aware of anything like that. The asp.net core validation documentation is very good, but is about how to use the validation features, not how it works internally. For that your best bet is to read the source code.

0reactions
pietia336commented, Nov 29, 2018

ASP.NET Core does not suffer from this issue as the validation infrastructure has been drastically improved there

Is there any documentation/book that you recommend to study to fully understand the changes in validation infrastructure of ASP.NET Core?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Partial server side validation for complex property
ModelState.Remove("ContactInfo");. Seems that the ModelState doesn't contain the "ContactInfo" key, so it will not work.
Read more >
Model validation in ASP.NET Core MVC and Razor Pages
Client -side validation prevents submission until the form is valid. The Submit button runs JavaScript that either submits the form or displays ...
Read more >
Client-side form validation - Learn web development | MDN
Before submitting data to the server, it is important to ensure all required form controls are filled out, in the correct format. This...
Read more >
ASP.Net MVC Server-Side Validation
The ASP.NET MVC Framework validates any data passed to the controller action that is executing, It populates a ModelState object with any ......
Read more >
Display Server Side Validation Errors with Angular - | juri.dev
There might be situations where you cannot find any matching form field, simply because the property is not shown, the field might be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found