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

Added support for collections of simple types (i.e. List<string>) #237

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

psampaio
Copy link

This follows @matthetherington 's implementation of PR #205 (for #204).

If you have a this model/validator:

public class PersonModel
{
    public required string Name { get; set; }
    public required List<string> AddressLines { get; init; }
}

public class PersonModelValidator : AbstractValidator<PersonModel>
{
    public PersonModelValidator()
    {
        RuleFor(m => m.Name)
            .NotEmpty()
            .WithMessage("Name is required.");

        RuleForEach(m => m.AddressLines)
            .NotEmpty()
            .WithMessage("Address {CollectionIndex} is required.");
    }
}

field validation is not working. Validation summary shows the errors, but you cannot link them to validation messages.

Here's what I believe is happening:

  • ValidateField is called, but the FieldIdentifier gives the property path as AddressLines.0
  • There's a mismatch with the validationResult.PropertyName, which is calculated as AddressLines[0] (same as the name tag if you use editContext.ShouldUseFieldIdentifiers = true).
  • .Where(validationFailure => validationFailure.PropertyName == propertyPath) fails

Assuming this analysis is correct, you can either 1. change the PropertyPath to have the AddressLines[0] format or 2. change the validationResult.PropertyName to have the AddressLines.0 format. 1 is happening somewhere inside ValidateAsync, but I couldn't figure out where, so I went with 2.

What I changed:

  • Handle the simple type collections in ToFieldIdentifier in case there are no more tokens and the remaining property path ends with "]"
  • Use that method to convert validationResult.PropertyName to the correct format and back to a property path.

I've done some testing, but I it's likely I'm missing some use cases.

@pwelter34
Copy link
Collaborator

pwelter34 commented Dec 6, 2024

Could you provide more of a sample to reproduce issue? Looking for the razor side of the code. thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants