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

[Bug] Version 2.1.0 generates System.InvalidOperationException #194

Open
sroy2020 opened this issue Jul 8, 2023 · 6 comments
Open

[Bug] Version 2.1.0 generates System.InvalidOperationException #194

sroy2020 opened this issue Jul 8, 2023 · 6 comments
Labels
Bug Something isn't working Triage Issue needs to be triaged

Comments

@sroy2020
Copy link

sroy2020 commented Jul 8, 2023

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?):

  • Blazor WebAssembly
  • .Net 6

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)]);
                });
            }
        }
    }
@sroy2020 sroy2020 added Bug Something isn't working Triage Issue needs to be triaged labels Jul 8, 2023
@Etnic
Copy link

Etnic commented Nov 20, 2023

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

@girlpunk
Copy link

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

@Etnic
Copy link

Etnic commented Nov 22, 2023

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.

@Nickprovs
Copy link

This is affecting my project as well.

@bonsall
Copy link

bonsall commented Jan 16, 2025

I'm seeing this as well. It looks like the issue is with complex model that contain an array (in my case a List) of complex model objects.

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

    public int Name { get; set; }
}

public class Bar
{
    public int Id { get; set; }
    public List<Foo> Foos { get; set; }
}

Then you render the enumeration inside of an EditContext with a FluentValidationValidator with the parent component as the model, and there is an input bound to a property on the child component.

<EditForm Model="BarInstance">
    <FluentValidationValidator />
    @* Probably a bunch of form fields here *@
    @foreach(var foo in BarInstance.Foos)
    {
        <InputText @bind-Value="foo.Name" />
    }
    
</EditForm>

Once you modify the input that is bound to that child components property you will get the exception. Looks like the validator is passing that child class instance to the parent validator and the validator is rightfully complaining that it can't validate that type.

@bonsall
Copy link

bonsall commented Jan 16, 2025

This also prevents validation messages from being cleared when the value changes. In this screenshot the age input is actually valid. If I try to submit the form or call EditContext.Validate the message will clear, just not when the value is updated.

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

5 participants