dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.36k stars 9.99k forks source link

MinLength DataAnnotation throws in WASM #19230

Closed marin-bratanov closed 4 years ago

marin-bratanov commented 4 years ago

Describe the bug

The [MinLength] data validation attribute works nicely for lists in server-side blazor, but throws in WASM blazor.

To Reproduce

This snippet works in server blazor, but not in WASM blazor

Repro steps:

  1. run
  2. click Validate
  3. click Add
  4. click Add two more times to satisfy the validator

Expected: works

Actual: throws on Add click in WASM

@using System.ComponentModel.DataAnnotations

<EditForm Model="@TheModel">
    <DataAnnotationsValidator />
    <ValidationSummary />
    <button type="submit">VALIDATE</button>
    <button @onclick="@AddStuff">add stuff</button>
</EditForm>

@code{
    TestModel TheModel { get; set; } = new TestModel();

    int counter { get; set; }
    void AddStuff()
    {
        if (TheModel.MyList == null)
        {
            TheModel.MyList = new List<string>();
        }
        TheModel.MyList.Add(counter++.ToString());
    }

    public class TestModel
    {
        [Required(ErrorMessage = "add something")]
        [MinLength(3, ErrorMessage = "At least three, actually")]
        public List<string> MyList { get; set; }
    }
}

Exception stack trace

blazor.webassembly.js:1 WASM: Unhandled exception rendering component:
blazor.webassembly.js:1 WASM: System.InvalidCastException: Specified cast is not valid.
blazor.webassembly.js:1 WASM:   at System.ComponentModel.DataAnnotations.MinLengthAttribute.IsValid (System.Object value) <0x323fd20 + 0x00042> in <filename unknown>:0 
blazor.webassembly.js:1 WASM:   at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid (System.Object value, System.ComponentModel.DataAnnotations.ValidationContext validationContext) <0x31ed3e0 + 0x0002e> in <filename unknown>:0 
blazor.webassembly.js:1 WASM:   at System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult (System.Object value, System.ComponentModel.DataAnnotations.ValidationContext validationContext) <0x31ed288 + 0x00026> in <filename unknown>:0 
blazor.webassembly.js:1 WASM:   at System.ComponentModel.DataAnnotations.Validator.TryValidate (System.Object value, System.ComponentModel.DataAnnotations.ValidationContext validationContext, System.ComponentModel.DataAnnotations.ValidationAttribute attribute, System.ComponentModel.DataAnnotations.Validator+ValidationError& validationError) <0x31ed008 + 0x00028> in <filename unknown>:0 

and so on

Further technical details

$ dotnet --info .NET Core SDK (reflecting any global.json): Version: 3.1.200-preview-014977 Commit: ca5522cead

Runtime Environment: OS Name: Windows OS Version: 10.0.18363 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.1.200-preview-014977\

Host (useful for support): Version: 3.1.1 Commit: a1388f194c

.NET Core SDKs installed: 3.1.101 [C:\Program Files\dotnet\sdk] 3.1.200-preview-014883 [C:\Program Files\dotnet\sdk] 3.1.200-preview-014977 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET Core runtimes or SDKs: https://aka.ms/dotnet-download

javiercn commented 4 years ago

@marin-bratanov thanks for contacting us.

Does it work if you disable the linker?

@pranavkm do you have any thoughts?

marin-bratanov commented 4 years ago

No, it doesn't.

I had tested initially with the linker disabled, sorry for not pointing this out in my initial post.

I would expect it to work both ways, though :)

pranavkm commented 4 years ago

This issue was moved to mono/mono#19022