简体   繁体   中英

FluentValidation .When() always false

Using FluentValidation 7.2.1 (asp.net mvc c#) as follows

public QuestionValidator()
{
    RuleFor(x => x.Answer).NotEmpty().When(x => 1 == 1);
}

the NotEmpty() rule does not render client side. If I remove the .When() it works fine. Note that I only use "1==1" to ensure that I forced a true condition to check it wasn't my property.

The 'real' code I was trying is as follows and .Required is true.

public QuestionValidator()
{
    RuleFor(x => x.Answer).NotEmpty().When(x => x.Required);
}

Reply from the author of fluentvalidation Although I'm still trying to find a way to have the required attribute set client side based on server side processing. of When() but not reacting to a change in the client side.

That behaviour is correct. Client-side validation does not support rules with When conditions. This is a limitation of MVC's clientside validation. Only a very limited subset of rules are supported clientsid. Please see this section of the documentation: https://github.com/JeremySkinner/FluentValidation/wiki/i.-ASP.NET-Core-integration#clientside-validation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM