Not all properties are validated server side on first submit
See original GitHub issueSystem 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.
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. 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. 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:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
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.
Is there any documentation/book that you recommend to study to fully understand the changes in validation infrastructure of ASP.NET Core?