1

I make a fluent validation class for this model

 public class OnlineCashierRequest
 {
    Guid InvoiceId { get; set; }      
    string ClientPhoneOrEmail { get; set; }    
    List<IOnlineCashierProduct> Products { get; set; }
 }

public interface IOnlineCashierProduct
{
     string Name { get; set; }
     decimal Amount { get; set; }
     int Count { get; set; }
}

I have a rule for products where Amount equals null. But how can I write in message row index like $"Amount is less zero for row {rowIndex}"

RuleForEach(t => t.Products)
    .Must(x => x.Amount < 0)
    .WithMessage(x => $"[{rowIndex}] Amount must be greater then zero\r\n");
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
user2455111
  • 234
  • 1
  • 9

1 Answers1

1

https://docs.fluentvalidation.net/en/latest/collections.html

RuleForEach(t => t.Products)
    .Must(x => x.Amount < 0)
    .WithMessage(x => $"[{CollectionIndex}] Amount must be greater than zero\r\n");
Simon H
  • 508
  • 1
  • 5
  • 18
  • Wouldn't it need to be `$"[{{CollectionIndex}}] Amount must be greater than zero\r\n"` because of the string interpolation? – Elias May 17 '23 at 09:53