Validate DateTime with FluentValidator

前端 未结 4 734
情书的邮戳
情书的邮戳 2020-12-31 02:25

This is my ViewModel class:

public class CreatePersonModel
{
    public string Name { get; set; }
    public DateTime DateBirth { get; set;          


        
4条回答
  •  半阙折子戏
    2020-12-31 02:49

    Have a look at the Fluent Validation documentation on GitHub:

    https://github.com/JeremySkinner/FluentValidation/wiki

    Try adding a RegEx Validator to ensure that the user's input (a string) can be parsed as a date correctly, prior to applying the Less Than Validator.

    EDIT

    Having run few test cases and looked at the source code for Fluent Validator I concede that the above approach won't work.

    The standard error you get is added during the Model Binding phase, which happens before the fluent validation framework can access and check the model.

    I assumed that the framework's authors had been clever and were injecting their validation code into the model binding phase. Looks like they aren't.

    So the short answer is what you want to do does not appear to be possible.

提交回复
热议问题