I need to do a specific validation with Fluent Validation, but I have read his documentation a few times and I did not find a solution for that. My validation class currently looks like this:
using FluentValidation;
using System;
namespace SaleTheaterTickets.Models.ViewModelValidators
{
public class GeneratedTicketViewModelValidator : AbstractValidator<GeneratedTicketViewModel>
{
public GeneratedTicketViewModelValidator()
{
RuleFor(x => x.CustomerName)
.NotEmpty().WithMessage("Digite o nome do cliente");
// .Matches("/ ^[a-zA-Z çÇÁáÉéÍíÓóÚúÃã']+$/i").WithMessage("Nome aceita apenas de A-Z");
RuleFor(x => x.BirthDate)
.NotEmpty().WithMessage("Digite a data de nascimento")
.Must(ValidDate).WithMessage("Data de nascimento tem que ser menor que hoje");
//.DependentRules(() => {
// RuleFor(x => x.NeedyChild)
// .Must(ValidQuestion).WithMessage("Resposta inválida: Você não é criança");
//});
RuleFor(x => x.FormOfPayment)
.NotEmpty().WithMessage("Selecione a forma de pagamento");
RuleFor(x => x.Seat)
.NotEmpty().WithMessage("Escolha a poltrona")
.NotNull().WithMessage("Poltrona inválida");
RuleFor(x => x.NeedyChild)
.NotEmpty().WithMessage("Responda a pergunta");
}
private static bool ValidDate(DateTime date)
{
if(date < DateTime.Now.Date)
{
return true;
}
return false;
}
//private static bool DiscountChild(DateTime date)
//{
// int age = DateTime.Now.Year - date.Year;
// if (age >= 2 && age <= 12)
// {
// return true;
// }
// return false;
//}
//private static bool ValidTotalChild(decimal Total)
//{
//}
//private static bool ValidQuestion( resposta)
//{
// int idade = DateTime.Now.Year - date.Year;
// bool _resposta = Convert.ToBoolean(resposta);
// if(_resposta == true)
// {
// if(idade >= 2 && idade <=12)
// {
// return true;
// }
// }
// return false;
//}
}
}
This system is for the sale of theater tickets, so I need the total to receive a 50% discount on the ticket value when the age of the people who are buying the ticket is between 2 and 12 years old and equal to or over 60 years old. In other words, children have a discount and the elderly too. However in my system I keep the birthday and not the age itself. I tried to use the must but the must returns bool (true or false) not allowing me to assign a value if the age is equal to both. can anybody help me?