'Fluent validation add rule with index
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");
Solution 1:[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");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Simon H |
