Как остановиться после первого сбоя при проверке notnull и иметь пользовательские / обязательные для поля с использованием беглого измерения? - PullRequest
0 голосов
/ 23 мая 2019
    Along with null error message, even the custom/must validation is ran and error failure is displayed. 

    I have set globally ValidatorOptions.CascadeMode = CascadeMode.StopOnFirstFailure;
    Inside Custom can check if(motor = null) return; But is there any other way to restrict not to run custom or must validation in fluent validator.

Below is my PersonDetails model class :
    Public Class PersonDetails {
     public PersonName {get;set;}
    }

    Public Class PersonName {
     public string Firstname {get;set;}
    public string LastName {get;set;}
    }


    public class PersonApplicantValidator : AbstractValidator<PersonDetails>
    {
                        RuleFor(x => x.PersonName ).NotNull().WithMessage("Mandatory field").Custom(
                            (personName, context) =>
                            {

                                if(personName.Firstname == null)
                                    context.AddFailure("FirstName is mandatory");
                            });

             } 

Когда PersonName = null, Актуально: Обязательное поле FirstName является обязательным

Expected : Mandatory field

Как остановить при первом сбое и не запускать кастом?

1 Ответ

0 голосов
/ 23 мая 2019

Чтобы сделать FirstName обязательным, только когда Name пусто, вы можете использовать это:

RuleFor(x => x.FirstName)
     .NotNull().When(x => string.IsNullOrEmpty(x.Name)).WithMessage("FirstName is mandatory");
...