Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate MemberToCompare and original #187

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions samples/BlazorServer/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@
<ValidationMessage For="@(() => _person.Address.Postcode)" />
</p>

<p>
<label>Start Lucky Number Range: </label>
<InputNumber @bind-Value="@_person.StartLuckyNumberRange" />
<ValidationMessage For="@(() => _person.StartLuckyNumberRange)" />
</p>

<p>
<label>End Lucky Number Range: </label>
<InputNumber @bind-Value="@_person.EndLuckyNumberRange" />
<ValidationMessage For="@(() => _person.EndLuckyNumberRange)" />
</p>

<button type="submit">Save</button>

</EditForm>
Expand Down
12 changes: 12 additions & 0 deletions samples/BlazorWebAssembly/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@
<ValidationMessage For="@(() => _person.Address.Postcode)" />
</p>

<p>
<label>Start Lucky Number Range: </label>
<InputNumber @bind-Value="@_person.StartLuckyNumberRange" />
<ValidationMessage For="@(() => _person.StartLuckyNumberRange)" />
</p>

<p>
<label>End Lucky Number Range: </label>
<InputNumber @bind-Value="@_person.EndLuckyNumberRange" />
<ValidationMessage For="@(() => _person.EndLuckyNumberRange)" />
</p>

<button type="submit">Save</button>

</EditForm>
Expand Down
5 changes: 5 additions & 0 deletions samples/Shared/SharedModels/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class Person
public int? Age { get; set; }
public string? EmailAddress { get; set; }
public Address Address { get; set; } = new();
public int StartLuckyNumberRange { get; set; }
public int EndLuckyNumberRange { get; set; }
}

public class PersonValidator : AbstractValidator<Person>
Expand Down Expand Up @@ -36,6 +38,9 @@ public PersonValidator()
.EmailAddress().WithMessage("You must provide a valid email address")
.MustAsync(async (email, _) => await IsUniqueAsync(email)).WithMessage("Email address must be unique");

RuleFor(p => p.StartLuckyNumberRange).LessThan(p => p.EndLuckyNumberRange).WithMessage("Start lucky number must be less than end lucky number");
RuleFor(p => p.EndLuckyNumberRange).GreaterThan(p => p.StartLuckyNumberRange).WithMessage("End lucky number must be greater than start lucky number");

RuleFor(p => p.Address).SetValidator(new AddressValidator());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FluentValidation;
using FluentValidation.Internal;
using FluentValidation.Validators;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.Extensions.DependencyInjection;
using static FluentValidation.AssemblyScanner;
Expand Down Expand Up @@ -76,15 +77,45 @@ private static async Task ValidateField(EditContext editContext,
{
var properties = new[] { fieldIdentifier.FieldName };
var context = new ValidationContext<object>(fieldIdentifier.Model, new PropertyChain(), new MemberNameValidatorSelector(properties));

validator ??= GetValidatorForModel(serviceProvider, fieldIdentifier.Model, disableAssemblyScanning);

if (validator is not null)
{
var validationResults = await validator.ValidateAsync(context);
var validationRules = validator as IEnumerable<IValidationRule>;
var members = new List<string>();

messages.Clear(fieldIdentifier);
messages.Add(fieldIdentifier, validationResults.Errors.Select(error => error.ErrorMessage));
if (validationRules != null)
{
foreach (var rule in validationRules)
{
var maybeComparison = rule.Components.FirstOrDefault()?.Validator;

if (maybeComparison != null && maybeComparison is IComparisonValidator comparisonValidator)
{
var compareTo = comparisonValidator.MemberToCompare.Name;
if (!members.Contains(compareTo) && rule.PropertyName == fieldIdentifier.FieldName)
{
members.Add(compareTo);
var newProperties = properties.ToList();
newProperties.Add(compareTo);
properties = newProperties.ToArray();
context = new ValidationContext<object>(fieldIdentifier.Model, new PropertyChain(), new MemberNameValidatorSelector(properties));
validationResults = await validator.ValidateAsync(context);
}
}
else
members.Add(fieldIdentifier.FieldName);
}
}

foreach (var member in members)
{
var field = new FieldIdentifier(fieldIdentifier.Model, member);
messages.Clear(field);
messages.Add(field, validationResults.Errors.Where(x => x.PropertyName == field.FieldName).Select(error => error.ErrorMessage));
}

editContext.NotifyValidationStateChanged();
}
Expand Down