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.21k stars 9.95k forks source link

Consider changing the behavior of text inputs in Blazor to treat empty string as null #51639

Open Eirenarch opened 11 months ago

Eirenarch commented 11 months ago

Is there an existing issue for this?

Is your feature request related to a problem? Please describe the problem.

Currently if the developer defines a text property as nullable (optional/not required) and the user touches the text input this triggers the validation and the user cannot remove the value. It is also impossible to remove the value in edit scenarios. This means that the developer must provide custom code to change every value from empty string to null. This is especially problematic when there is a min length validation.

Describe the solution you'd like

Consider changing the behavior of text inputs in Blazor to automatically convert empty strings to null

While this suggestion is a breaking change I feel like it will be beneficial because it is hard to imagine many people want the empty string behavior. I also believe that ASP.NET MVC textboxes treat empty strings as null so there will be some consistency. Alternatively there might be a switch somewhere to turn on the null behavior. Another alternative is to provide another control with the new behavior. While creating such a control is not hard it will be good to not have to write it for every project.

Additional context

This issue has been raised before but closed without consideration - https://github.com/dotnet/aspnetcore/issues/40785

ghost commented 11 months ago

Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

ghost commented 9 months ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

thhunt commented 7 months ago

Yes, this please. The same sort of functionality exists with ASP .Net Core / MVC where POST back to the controller from a form maps empty string to null for nullable properties. This would prevent a lot of manual code on either model properties or component event handlers.

kgar commented 2 months ago

Adding to the discussion:

I'm working on a Blazor application where the database expects null or a well-formed phone number string. The db column has a check constraint on it. This is an established database over which I have no control.

Using a Blazor EditForm and binding to a nullable string results in an empty string when I clear out a phone number, for example. This breaks validation, and if validation is bypassed, it triggers a check constraint error on attempting to save the data.

The workaround I'm using is to update the relevant properties to convert empty string to null in the setter.

That Blazor project is going to scale quite a bit, so I can expect to see this workaround propagated across hundreds of models.