Skip to content

MudFormComponent: Fix validation handling (#9215)#9386

Merged
henon merged 3 commits intoMudBlazor:devfrom
SKSniperSK:fix/mudformcomponent-multiple-validations
Jul 15, 2024
Merged

MudFormComponent: Fix validation handling (#9215)#9386
henon merged 3 commits intoMudBlazor:devfrom
SKSniperSK:fix/mudformcomponent-multiple-validations

Conversation

@SKSniperSK
Copy link
Contributor

@SKSniperSK SKSniperSK commented Jul 12, 2024

Description

Fixes validation handling in MudFormComponent:

  • Prioritize validation context of the more specific input component over the more general one from the surrounding form.
  • Skip 'internal' validation when there is an EditContext because all generated validation messages will get overwritten by OnValidationStateChanged once the validation handler of the EditContext is updating the validation state. This also prevents 'double validation' and possible issues with custom validators that might get executed twice and as a bonus there is a minimal performance gain as unneeded code parts are skipped.

Fixes #9215

How Has This Been Tested?

All existing unit tests have passed and manual testing of the forms section in MudBlazor.Docs.Server has been done.

Type of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation (fix or improvement to the website or code docs)

Checklist

  • The PR is submitted to the correct branch (dev).
  • My code follows the code style of this project.
  • I've added relevant tests. -> not needed, existing tests already cover form validation

@github-actions github-actions bot added bug Unexpected behavior or functionality not working as intended PR: needs review labels Jul 12, 2024
@codecov
Copy link

codecov bot commented Jul 12, 2024

Codecov Report

Attention: Patch coverage is 40.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 90.59%. Comparing base (28bc599) to head (fbfa7fb).
Report is 346 commits behind head on dev.

Files Patch % Lines
src/MudBlazor/Base/MudFormComponent.cs 40.00% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #9386      +/-   ##
==========================================
+ Coverage   89.82%   90.59%   +0.76%     
==========================================
  Files         412      403       -9     
  Lines       11878    12669     +791     
  Branches     2364     2448      +84     
==========================================
+ Hits        10670    11478     +808     
+ Misses        681      632      -49     
- Partials      527      559      +32     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ScarletKuro ScarletKuro requested a review from henon July 12, 2024 17:31
Copy link
Contributor

@henon henon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this change seems to make a lot of sense

@ScarletKuro
Copy link
Member

Before I merge it, do we need any notes on little behavior change for migration guide or not or we just consider it as bug fix?

@henon
Copy link
Contributor

henon commented Jul 13, 2024

I'd argue this is a bug fix. However I suggested a clarification note in the docs of EditForm #9215 (comment)

@henon henon merged commit ea5b8cd into MudBlazor:dev Jul 15, 2024
@henon
Copy link
Contributor

henon commented Jul 15, 2024

Thanks!

@SKSniperSK SKSniperSK deleted the fix/mudformcomponent-multiple-validations branch July 15, 2024 13:38
@elylv
Copy link

elylv commented Jul 18, 2024

Will this fix an issue we're seeing with Mud input components that have Required=true within an EditForm (not MudForm) with a custom validator? Anything with required=true pops up a validation error rather than running our own fluent validator which does more conditional validation (and could skip required fields in certain instances)?

@ScarletKuro
Copy link
Member

Will this fix an issue we're seeing

I would say the best thing is to try it out. It's part of MudBlazor starting from version 7.2.0.

@elylv
Copy link

elylv commented Jul 18, 2024

Hmm, didn't realise it was already in release, in which case, it does not. I'll need to raise a bug.

@ScarletKuro
Copy link
Member

Mud input components that have Required=true within an EditForm (not MudForm) with a custom validator? Anything with required=true pops up a validation error rather than running our own fluent validator which does more conditional validation (and could skip required fields in certain instances)?

I don't think it will, based on my knowledge. When you set Require, it adds a required attribute to the underlying native HTML inputs. When used in conjunction with a form, the default HTML browser validation will be triggered, and this validation will always dominate, as in this case: #9409 (comment). Unfortunately, I don't know if this validation can be suppressed.

@ScarletKuro
Copy link
Member

Hmm, didn't realise it was already in release, in which case, it does not. I'll need to raise a bug.

Not sure if it's even possible to fix tho.

@ScarletKuro
Copy link
Member

I think you can try to add the novalidate attribute to the EditForm

@elylv
Copy link

elylv commented Jul 18, 2024

I think you can try to add the novalidate attribute to the EditForm

OK, I guess with v7, MudBlazor adds the required HTML tag where previously it didn't (and just appended * to the label to denote required). Looks like adding the novalidate overrides the native handing of required. Thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Unexpected behavior or functionality not working as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MudFormComponent:ValidateWithAttribute ValidationContext prioritization

4 participants