Blazored / FluentValidation

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

[Bug] Validating a single property triggers validation for all rules #191

Closed chrisportelli closed 1 year ago

chrisportelli commented 1 year ago

Describe the bug In a vanilla .NET 6 Blazor Server project, I am using Blazored.FluentValidation 2.1.0. When trying to validate a single property, as documented in the FluentValidation docs, it is triggering the validation for the whole form.

I've tried to create a .NET 6 MVC project and used FluentValidation 11.5.2 directly. As expected, only the provided property is being validated when I call Validate() by specifying the property in the IncludeProperties() method. Is there something wrong that I'm doing or missing from my side on Blazor Server?

To Reproduce

  1. Create a FormModel.cs class
public class FormModel
{
    public string? Name { get; set; }
    public string? Email { get; set; }
    public string? Comment { get; set; }
}
  1. Create a FormModelValidator.cs class
public class FormModelValidator : AbstractValidator<FormModel>
{
    public FormModelValidator()
    {
        RuleFor(x => x.Name).NotEmpty().WithMessage("Name is mandatory");
        RuleFor(x => x.Email).NotEmpty().EmailAddress().WithMessage("Email is mandatory and must be valid");
        RuleFor(x => x.Comment).NotEmpty().WithMessage("Comment is mandatory");
    }
}
  1. Register validation class in Program.cs
builder.Services.AddValidatorsFromAssemblyContaining<FormModelValidator>();
  1. Create form in a Razor page
<EditForm Model="@_model">
    <FluentValidationValidator @ref="_fluentValidationValidator" DisableAssemblyScanning="@true" />

    <div class="form-group">
        <label for="name">Name:</label>
        <InputText id="name" class="form-control" @bind-Value="@_model.Name" />
        <ValidationMessage For="@(() => _model.Name)" />
    </div>

    <div class="form-group">
        <label for="email">Email:</label>
        <InputText id="email" class="form-control" @bind-Value="@_model.Email" />
        <ValidationMessage For="@(() => _model.Email)" />
    </div>

    <div class="form-group">
        <label for="comment">Comment:</label>
        <InputTextArea id="comment" class="form-control" @bind-Value="@_model.Comment" />
        <ValidationMessage For="@(() => _model.Comment)" />
    </div>

    <button type="submit" class="btn btn-primary" @onclick="@HandleSubmit">Submit</button>
</EditForm>

@if (_formSubmitted)
{
    <p class="text-success">Form submitted successfully!</p>
}

@if (_formError)
{
    <p class="text-danger">Form is incorrect!</p>
}

@code {
    private FormModel _model = new FormModel();
    private bool _formSubmitted, _formError;
    private FluentValidationValidator? _fluentValidationValidator;

    private async Task HandleSubmit()
    {
        var isValid = await _fluentValidationValidator!.ValidateAsync(options => options.IncludeProperties("Comment"));
        if (isValid)
        {
            _formSubmitted = true;
        }
        else
        {
            _formError = true;
        }
    }
}

Expected behavior A clear and concise description of what you expected to happen.

Screenshots Animation

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

Additional context Add any other context about the problem here.

vip32 commented 1 year ago

isn't this expected behavior? on submit the WHOLE model is validated

chrisportelli commented 1 year ago

Closing this as this has been resolved via this StackOverflow post.