Blazored / FluentValidation

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

[Bug] Version 2.1.0 generates System.InvalidOperationException #194

Open sroy2020 opened 1 year ago

sroy2020 commented 1 year ago

Describe the bug I get the following exception (see screenshot 1) when using version 2.1.0 but everything is fine with 2.0.3. 1

To Reproduce Steps to reproduce the behavior: When using 2.0.3 I just click on a dropdown that will specify the attachment to open. The list used by drop-down is not even part of the model. 2

Expected behavior No exception, just like with version 2.0.3. I'm using an EditContext (no choice here, it must be an EditContext) instead of an EditForm.

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

Additional context TabGeneral.razor

@inherits OrderTabComponentBase<OrderEntryUI.Models.OrderGeneralModel>

@using OrderEntryUI.Extensions
@using System
@using System.Text
@using System.Threading.Tasks
@using Telerik.FontIcons

<div class="container-fluid mt-1 mb-3">
    <TelerikForm EditContext="@TabContext">
        <FormValidation>
            <ObjectGraphDataAnnotationsValidator />
            <FluentValidationValidator Validator="@Validator">
            </FluentValidationValidator>
        </FormValidation>
...
  <FormItem>
    <Template>
        <div class="@ValidationMessageOriginalOrder.GetCss()">
            <OrderEntryInput InputType="@OrderEntryInput.InputTypeId.Text"
                         ReadWriteMode="@Orders.IsPermissionGranted(
                         Pages.Orders.OrderPermissionTypes.CanTabBeUpdated, Id)"
                         CssColLabel="col-4" Label="@Model.LabelOriginalOrderNo"
                         CssColValue="col-7" @bind-ValueText="@Model.OriginalOrder"
                         UpdatedAsync="UpdatedAsync">
            </OrderEntryInput>
            <div class="row">
                <div class="col-4"></div> @* Needed so it's aligned with the input. *@
                <div class="col-7">
                    <div class="d-flex justify-content-start">
                        <TelerikValidationMessage For="@(() => Model.OriginalOrder)"
                                              @ref="ValidationMessageOriginalOrder"></TelerikValidationMessage>
                    </div>
                </div>
            </div>
        </div>
    </Template>
  </FormItem>
...
CLICKING ON THIS DROP DOWN GENERATES THE EXCEPTION
      <div class="col-4 ms-5 oe-general-attachment-list">
          <AttachmentList OrderNo="@Model.OrderNo" OrderAttachments="@OrderAttachments"
                          OrderTab="@this">
          </AttachmentList>
      </div>
...

TabGeneral.razor.cs

namespace OrderEntryUI.Components.Orders
{
    using Microsoft.AspNetCore.Components.Forms;
    using OrderEntryUI.Extensions;
    using OrderEntryUI.Models;
    using OrderEntryWS.DTO;
    using System;
    using System.Threading.Tasks;
    using Telerik.Blazor.Components;

    public partial class TabGeneral
    {
        public enum ExtraUpdate
        {
            None = 0
        }

        public override IOrderTab.TabId Id => IOrderTab.TabId.General;

        private OrderGeneralModel.Validator Validator { get; set; } =
            new OrderGeneralModel.Validator();
...

AttachmentList .razor

...
    <OrderEntryInput InputType="@OrderEntryInput.InputTypeId.DropDownIntValue"
                     ReadWriteMode="true"
                     CssColLabel="col-3" Label="@LocCommon[nameof(Resources.Common.Attachments)]"
                     CssColValue="col-9" @bind-ValueDropDownIntData="@AttachmentId"
                     CssDropDownData="oe-dropdown-attachments"
                     DropDownIntData="@DropDownAttachmentEntries"
                     UpdatedAsync="UpdatedAsync">
    </OrderEntryInput>
...

AttachmentLis.razor.cs:

namespace OrderEntryUI.Components.Orders.Widgets
{
    using Microsoft.AspNetCore.Components;
    using Microsoft.Extensions.Localization;
    using Microsoft.JSInterop;
    using OrderEntryUI.Helpers;
    using OrderEntryUI.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;

    public partial class AttachmentList
    {
        [Inject]
        public IStringLocalizer<Resources.Orders> Loc { get; set; }

        [Inject]
        private IJSRuntime Js { get; set; }

        [Parameter]
        public int OrderNo { get; set; }

        [Parameter]
        public List<OrderAttachmentData> OrderAttachments { get; set; }

Model

public class OrderGeneralModel : OrderCommonModel
{
...  
        public string LabelOriginalOrderNo { get; set; }
        public string OriginalOrder { get; set; }
        public string LabelCustomerPO { get; set; }
,,,
        public string LabelBOLPO { get; set; } // to use if LabelCustomerPO null or empty
        public string CustomerPO { get; set; }
        public int PORequired { get; set; }
...
...

        public class Validator : AbstractValidator<OrderGeneralModel>
        {
            public Validator()
            {
                var loc = new OrderMessagesLocalizer();

                When(m => !string.IsNullOrWhiteSpace(m.LabelOriginalOrderNo), () =>
                {
                    RuleFor(m => m.OriginalOrder).NotEmpty()
                        .WithMessage(loc[nameof(Resources.Orders.OriginalOrderNumberIsRequired)]);
                });

                // TODO: To review with André
                When(m => m.PORequired == 1 && (!string.IsNullOrWhiteSpace(m.LabelCustomerPO) ||
                    !string.IsNullOrWhiteSpace(m.LabelBOLPO)), () =>
                {
                    RuleFor(m => m.CustomerPO).NotEmpty()
                        .WithMessage(loc[nameof(Resources.Orders.CustomerPORequired)]);
                });
            }
        }
    }
Etnic commented 11 months ago

Same for me...Is there a fix for this problem?

girlpunk commented 11 months ago

As a workaround, downgrading to 2.0.3 has been effective for me

Etnic commented 11 months ago

As a workaround, downgrading to 2.0.3 has been effective for me

Yes, in 2.0.3 it works. Hope in a new release it will be fixed.