Skip to content
This repository was archived by the owner on Dec 26, 2025. It is now read-only.
This repository was archived by the owner on Dec 26, 2025. It is now read-only.

[Question] Pass an argument to Validator #251

@yongkeecho

Description

@yongkeecho

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

No one assigned

    Labels

    QuestionQuestion about this projectTriageIssue needs to be triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions