コード例 #1
0
 public void SetupValidationRules(ModelValidator <ProductSpecificationModel> v)
 {
     v.RuleFor(x => x.Name).NotEmpty().When(x => x.Id == 0);
     v.RuleFor(x => x.Values).Custom((list, context) =>
     {
         if (!list.Any())
         {
             context.AddFailure(nameof(ProductSpecificationValueModel.AttributeValue), "At least one specification value must be provided");
         }
     });
     v.RuleForEach(x => x.Values)
     .Custom((model, context) =>
     {
         if (model.AttributeValue.IsNullEmptyOrWhiteSpace())
         {
             context.AddFailure(nameof(ProductSpecificationValueModel.AttributeValue), "Can't add empty values");
         }
     });
 }
コード例 #2
0
 public void SetupValidationRules(ModelValidator <ProductAttributeModel> v)
 {
     v.RuleFor(x => x.Name).NotEmpty().When(x => x.Id == 0);
     //v.RuleForEach(x => x.Values).SetValidator(new ModelValidator<ProductAttributeValueModel>());
     v.RuleFor(x => x.Values).Custom((list, context) =>
     {
         var instance = context.InstanceToValidate as ProductAttributeModel;
         if (instance.InputFieldType.RequireValues() && !list.Any())
         {
             context.AddFailure(nameof(ProductAttributeValueModel.AttributeValue), "At least one attribute value must be provided");
         }
     });
     v.RuleForEach(x => x.Values)
     .Custom((model, context) =>
     {
         if (model.AttributeValue.IsNullEmptyOrWhiteSpace())
         {
             context.AddFailure(nameof(ProductAttributeValueModel.AttributeValue), "Can't add empty values");
         }
     });
 }