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

Blazor Form Validation Input elements initially rendered with class valid #27113

Closed AmarjeetBanwait closed 10 months ago

AmarjeetBanwait commented 3 years ago

Current Behaviour:

Expected Behaviour:

This problem arising if we replace valid/invalid class names with Bootstrap's is-valid/is-invalid class names using MyFieldClassProvider : FieldCssClassProvider

Another Issue:

.valid.modified:not([type=checkbox]) { outline: 1px solid #26b050; }

Expected Behaviour:

validations

vertonghenb commented 3 years ago

I think I mentioned this before in the PR https://github.com/dotnet/aspnetcore/pull/24835#issuecomment-698009470

AmarjeetBanwait commented 3 years ago

@vertonghenb Yes but I think @SteveSandersonMS missed the case when element will be rendered with initial value.

https://github.com/dotnet/aspnetcore/pull/24835#issuecomment-698965130 will work fine if element is rendered without initial value but .valid without .modified must apply styles if field is rendered with some value So in the PR you mentioned we also need to check if the field has initial value

return (isModified, isValid, hasInitialValue) switch {
             (true, true, true) => "modified valid",        //if modified then no need to check initialValue
             (true, true, false) => "modified valid",       
             (true, false, true) => "modified invalid",
             (true, false, false) => "modified invalid",

             (false, true, true) => "pristine valid",        //if not modified & has initialValue then only valid/invalid class must apply styles
             (false, false, true) => "pristine invalid",

             (false, true, false) => "pristine",       //if not modified & has no initialValue then only pristine is enough
              (false, false, false) => "pristine"      
mkArtakMSFT commented 3 years ago

Thanks for contacting us, @AmarjeetBanwait. Is there any specific issue you're trying to emphasize here? The behavior seems to be inline with our expectations.

AmarjeetBanwait commented 3 years ago

This is the Issue. I am not able to distinguish between an element rendered without value and an element with some initial value. In both cases valid class will be applied without modified. So I need to check if element has value or not eg. editContext.HasValue(fieldIdentifier); From this it will be easy to find that an element has InitialValue or not.

Also It makes no sense to apply valid class to empty element. As described in description above.

SteveSandersonMS commented 3 years ago

Also It makes no sense to apply valid class to empty element

Doesn’t that depend on whether an empty string is allowed for the field?

I am not able to distinguish between an element rendered without value and an element with some initial value

What field type are you thinking of? For an int, .NET doesn’t have any representation for ‘no value’, so you’d need to use a nullable int instead. Then in that case empty string is valid by default, but would be invalid if you marked the field as required.

AmarjeetBanwait commented 3 years ago

Doesn’t that depend on whether an empty string is allowed for the field?

Doesn't empty string should be invalid for a required property. Shouldn't nullable int be invalid initially.

I am already using nullable int and required properties Sample

and every field is valid initially

ghost commented 3 years 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.

Bonelol commented 3 years ago

Same issue here, the intial css class is 'valid'. Is it possible to change EditContext.GetFieldState to public, or we need a method in either EditContext or EditForm to mark fields as modified.

SteveSandersonMS commented 3 years ago

we need a method in either EditContext or EditForm to mark fields as modified

Please see https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.forms.editcontext.notifyfieldchanged?view=aspnetcore-5.0

wobuzhudao commented 3 years ago

Seeing this issue when I have a text-type input that has the required attribute applied. If I clear the text (make it empty), the valid.modified class is applied.

It should apply the invalid class instead (since the textbox is now empty, and the input is required).

ghost commented 10 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.

mkArtakMSFT commented 10 months ago

This is a dupe of https://github.com/dotnet/aspnetcore/issues/18884