admin管理员组文章数量:1278691
Is there a way to return multiple messages on one rule?
public class EmployeeValidator : AbstractValidator<Employee>
{
public EmployeeValidator ()
{
RuleFor(p => p.StartDate).Custom(ValidateStartDate);
}
private void ValidateStartDate(DateTime? startDate, ValidationContext<Employee> context)
{
var gap = context.InstanceToValidate;
if(startDate != null && startDate > DateTime.Noew.Date)
{
context.AddFailure(new ValidationFailure(nameof(gap.StartDate), "Start Date mustn't be in future."); // Message 1
}
if(condition)
{
context.AddFailure(new ValidationFailure(nameof(gap.StartDate), "Employee must be registered on this date."); // Message 2
}
}
}
In the Blazor app:
<MudDatePicker Label="Start Date" @bind-Date="employee.StartDate" Mask="@(new DateMask("MM/dd/yyyy"))"
DateFormat="MM/dd/yyyy" ShowToolbar="false"
Variant="Variant.Outlined" Margin="Margin.Dense" Editable="true" For="() => employee.StartDate"></MudDatePicker>
This is always displaying message 2. Is it possible to display both the messages at once?
Is there a way to return multiple messages on one rule?
public class EmployeeValidator : AbstractValidator<Employee>
{
public EmployeeValidator ()
{
RuleFor(p => p.StartDate).Custom(ValidateStartDate);
}
private void ValidateStartDate(DateTime? startDate, ValidationContext<Employee> context)
{
var gap = context.InstanceToValidate;
if(startDate != null && startDate > DateTime.Noew.Date)
{
context.AddFailure(new ValidationFailure(nameof(gap.StartDate), "Start Date mustn't be in future."); // Message 1
}
if(condition)
{
context.AddFailure(new ValidationFailure(nameof(gap.StartDate), "Employee must be registered on this date."); // Message 2
}
}
}
In the Blazor app:
<MudDatePicker Label="Start Date" @bind-Date="employee.StartDate" Mask="@(new DateMask("MM/dd/yyyy"))"
DateFormat="MM/dd/yyyy" ShowToolbar="false"
Variant="Variant.Outlined" Margin="Margin.Dense" Editable="true" For="() => employee.StartDate"></MudDatePicker>
This is always displaying message 2. Is it possible to display both the messages at once?
Share Improve this question edited Feb 25 at 0:38 blue pink asked Feb 24 at 23:11 blue pinkblue pink 211 bronze badge 8 | Show 3 more comments1 Answer
Reset to default 0You could try this below code:
let's say I have two condition first will check the null date and second will check the grater than today's day
using FluentValidation;
namespace BlazorApp2
{
public class EmployeeValidator : AbstractValidator<Employee>
{
public EmployeeValidator()
{
RuleFor(p => p.StartDate).Custom(ValidateStartDate);
}
private void ValidateStartDate(DateTime? startDate, ValidationContext<Employee> context)
{
var employee = context.InstanceToValidate;
// First Validation: Start Date mustn't be in future.
if (startDate != null)
{
context.AddFailure(nameof(employee.StartDate), "Start Date mustn't be in future.");
}
// Second Validation: Employee must be registered on this date.
if (startDate > DateTime.Now.Date)
{
context.AddFailure(nameof(employee.StartDate), "Employee must be registered on this date.");
}
}
}
}
OR
One condition will check future date selection and second will show past selection:
// First Validation: Start Date mustn't be in future.
if (startDate != null && startDate > DateTime.Now.Date)
{
context.AddFailure(nameof(employee.StartDate), "Start Date mustn't be in future.");
}
// Second Validation: Employee must be registered on this date.
if (startDate != null && startDate < DateTime.Now.AddYears(-1).Date)
{
context.AddFailure(nameof(employee.StartDate), "Employee must be registered on this date.");
}
razor page:
@page "/"
@using FluentValidation
@using MudBlazor
@inject IValidator<Employee> EmployeeValidator
<EditForm Model="@employee" OnValidSubmit="HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<MudForm @ref="form" Model="@employee">
<MudTextField Label="Name" @bind-Value="employee.Name" />
<MudDatePicker Label="Start Date" @bind-Date="employee.StartDate" Mask="@(new DateMask("MM/dd/yyyy"))"
DateFormat="MM/dd/yyyy" ShowToolbar="false"
Variant="Variant.Outlined" Margin="Margin.Dense" Editable="true">
</MudDatePicker>
<MudButton OnClick="ValidateForm" Variant="Variant.Filled" Color="Color.Primary">Submit</MudButton>
</MudForm>
</EditForm>
@code {
private Employee employee = new Employee();
private MudForm form;
private async Task ValidateForm()
{
var result = await EmployeeValidator.ValidateAsync(employee);
if (result.IsValid)
{
Console.WriteLine("Form Submitted Successfully!");
}
else
{
// Log all validation errors
foreach (var failure in result.Errors)
{
Console.WriteLine(failure.ErrorMessage);
}
}
}
private void HandleValidSubmit()
{
// Handle submission logic after the form is valid
Console.WriteLine("Form submitted successfully.");
}
}
Let me know if you have any concern regarding the provided information or my understanding is different
本文标签: blazorFluent ValidationMultiple messages for one ruleStack Overflow
版权声明:本文标题:blazor - Fluent Validation - Multiple messages for one rule - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741236993a2363212.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var errorlist = new List<string>(); if (startDate != null && startDate > DateTime.Now.Date) { errorlist.Add("Start Date mustn't be in future."); } if (condition) { errorlist.Add("Employee must be registered on this date."); } if (errorlist.Count > 0) { var errormessage = string.Join("<br/>", errorlist); context.AddFailure(new ValidationFailure(nameof(gap.StartDate), errormessage)); }
– Jalpa Panchal Commented Feb 26 at 10:56