This repository was archived by the owner on Dec 26, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 93
This repository was archived by the owner on Dec 26, 2025. It is now read-only.
[Question] Pass an argument to Validator #251
Copy link
Copy link
Open
Labels
QuestionQuestion about this projectQuestion about this projectTriageIssue needs to be triagedIssue needs to be triaged
Description
Please be sure to check the readme file before raising an issue.
Is there a way to pass an argument to a validator? My ScheduleValidator needs to check its StartTime is past or not. So I need to convert UtcNow to user's local time as below. How can I set Offset from my Blazor code?
`public class ScheduleValidator : AbstractValidator
{
public TimeSpan Offset { get; set; } = TimeSpan.Zero;
public ScheduleValidator()
{
RuleFor(s => s.Subject)
.NotEmpty().WithMessage("You must provide a subject.")
.MaximumLength(64).WithMessage("The subject exceeds the allowed length of 64.");
RuleFor(s => s.ReservedBy)
.NotEmpty().WithMessage("You must specify who is reserving the event.")
.EmailAddress().WithMessage("You must provide a valid Gilead email address.");
RuleFor(s => s.StartTime)
.GreaterThanOrEqualTo(GetLocalNow()).WithMessage("You cannot book an event in the past.")
.LessThanOrEqualTo(GetLocalNow().AddMonths(1)).WithMessage("You cannot book more than a month in advance.");
RuleFor(s => s.Description)
.NotEmpty().WithMessage("You must provide a short description of the event.")
.MaximumLength(256).WithMessage("The description exceeds the allowed length of 256.");
}
public DateTime GetLocalNow() => new DateTime(new DateTimeOffset(DateTime.UtcNow).ToOffset(Offset).Ticks, DateTimeKind.Local);
}`
`
<FluentValidationValidator @ref="_fluentValidator" DisableAssemblyScanning="false" />
@code {
private FluentValidationValidator? _fluentValidator;
private async Task OnSubmitAsync()
{
if (await _fluentValidator!.ValidateAsync())
{
...
`
Metadata
Metadata
Assignees
Labels
QuestionQuestion about this projectQuestion about this projectTriageIssue needs to be triagedIssue needs to be triaged