Blazored / FluentValidation

A library for using FluentValidation with Blazor
https://blazored.github.io/FluentValidation/
MIT License
587 stars 84 forks source link

[Bug] System.InvalidOperationException: Could not find property at Blazored.FluentValidation.EditContextFluentValidationExtensions.ToFieldIdentifier #43

Closed Liero closed 3 years ago

Liero commented 3 years ago

Describe the bug Calling StateHasChanged on the component causes InvalidOperatorException when there is RuleForEach validation rule in the Validator. Additionally the validation error disappears and input is marked as Valid.

To Reproduce

  1. Run following code
  2. Removed text from a cell in Name column to cause validation error
  3. Click the button
<EditForm Model="this">    
    <Blazored.FluentValidation.FluentValidationValidator />
    <table>
        <tbody>
            @foreach (var item in DataSource)
            {
                <tr @key="item">
                    <td><InputNumber @bind-Value="item.Id" /></td>
                    <td>
                        <InputText @bind-Value="item.Name" />
                        <ValidationMessage For="() => item.Name" />
                    </td>
                </tr>
            }
        </tbody>
    </table>
    <button @onclick="this.StateHasChanged">Click</button>
</EditForm>

@code {
    public List<Item> DataSource = Enumerable.Range(1, 10).Select(i => new Item { Id = i, Name = $"Item {i}" }).ToList();

    public class Item
    {
        public int Id { get; set; }

        [Required]
        [StringLength(10, MinimumLength = 2)]
        public string Name { get; set; }
    }

    public class MyValidator : AbstractValidator<Index>
    {
        public MyValidator()
        {
            //RuleFor(m => m.TestProp).NotEmpty().MinimumLength(2);
            RuleForEach(m => m.DataSource).SetValidator(new ItemValidator());
        }
    }

    public class ItemValidator : AbstractValidator<Item>
    {
        public ItemValidator()
        {
            RuleFor(m => m.Name).NotEmpty().MinimumLength(2);
        }
    }
} 

Expected behavior After clicking the button, all invalid Inputs should be marked as invalid and validation messages should be shown.

Hosting Model (is this issue happening with a certain hosting model?):

Additional context .NET 5 RC2

chrissainty commented 3 years ago

I'm not sure what to make of this. Why would you have a button which calls StateHasChanged? Surely you should be submitting the form? At this point I can't consider this a bug as the code is not a real world scenario.

If you have a real world scenario where the library is erroring then please feel free to reopen the issue and provide a working repro of the problem.