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

Validation message for field does not show error messages from custom validation attributes #20038

Closed juliushuck closed 4 years ago

juliushuck commented 4 years ago

Describe the bug

Asp.Net Core Blazor WASM

I created a custom validation attribute and overridden the IsValid function. The validation summary shows me the error message correctly but the validation message does not show the error. And the textbox is getting green. It is working for standard validation attributes.

<EditForm Model="@_createModel" OnValidSubmit="CreateProjectValidSubmitted" Context="EditFormContext">
    <DataAnnotationsValidator></DataAnnotationsValidator>
    <div class="form-group">
        <DxTextBox @bind-Text="@_createModel.Name" NullText="Name"></DxTextBox>
        <ValidationMessage For="@(() => _createModel.Name)"></ValidationMessage>
    </div>
    <DxButton Text="Create" SubmitFormOnClick="true" RenderStyle="@ButtonRenderStyle.Primary" />
    <ValidationSummary></ValidationSummary>
</EditForm>

I discovered a strange behavior. When I open the page and submit the empty form, No error is shown in the validation message. When I fill in sth wrong and submit it, it submits valid. When I remove the data and submit, it does not submit valid and shows me the correct errors. When I click submit again on the empty form the errors are hidden and the textbox is green but the form does not get submitted valid.

Further technical details

.NET Core SDK (reflecting any global.json): Version: 3.1.101 Commit: b377529961

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.101\

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

.NET Core SDKs installed: 3.1.101 [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.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

Rider by JetBrains Windows

pranavkm commented 4 years ago

Do your properties have RequiredAttribute? Most data annotations validations do not execute if the value is null:

https://github.com/dotnet/runtime/blob/master/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/StringLengthAttribute.cs#L51-L56

ghost commented 4 years ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.

juliushuck commented 4 years ago

No they are custom attributes. The standard ones are working. But I'm checking for null and the required type

pranavkm commented 4 years ago

If it works with regular validation attributes, do you think there's an issue with how you've authored the attributes. If you don't think this is an issue with your code, could you share a minimal app that reproduces this issue?

ghost commented 4 years ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.