FluentValidation icon indicating copy to clipboard operation
FluentValidation copied to clipboard

When used with DynamicComponent(.Net 6 feature) validation messages are getting duplicated

Open umeshvenkat opened this issue 4 years ago • 7 comments

I am trying to build a Wizard feature using DynamicComponent(.Net 6 feature). When we navigate to next page and comeback using previous button and clear the text of some control and make to validate the form, validation fires. As many times we repeat this, validation messages repeats as shown in below image. Expectation is to show one message. Complete code is available in following repo.

https://github.com/umeshvenkat/DynamicComponentFluentValidation

This is how repetitive messages are displayed.

image

umeshvenkat avatar Oct 08 '21 10:10 umeshvenkat

I think its happening because multiple validators getting attached when we traverse using next and previous button. Is there a way to add validator only once.

Same setup works fine with Data Annotation Validators without duplicating validation messages.

umeshvenkat avatar Oct 12 '21 05:10 umeshvenkat

Thanks for raising this @umeshvenkat. I've haven't done any work with the Dynamic component so I'm not sure what kind of effect it would have. It seems the validation message store isn't being cleared for some reason when a new validation call is made. I think this will take me some time to get my head around.

chrissainty avatar Oct 22 '21 06:10 chrissainty

It looks like my #155 issue

alexx-grand avatar Aug 01 '22 14:08 alexx-grand

Hi, @chrissainty are you any closer to finding a fix or work around for this?

DanRogers99 avatar Nov 11 '22 10:11 DanRogers99

There is another problem due to the reuse of the same EditContext, for example when some components with conditions are shown or hidden.

I have created a modal window which has two EditForm components. These are each separated by an If block and can be shown or hidden with a ToggleButton. The first form uses its own EditContext while the second references a simple class.

Now when I edit a field in the first form it is initially shown as valid, but if I delete the entry so that the field is marked as edited then the NotEmpty rule kicks in and shows this red with the appropriate error message. Now I switch to the second form and right back while the error message was still displayed. Right when I call the first form again, the error message and the red border around the text field remains. If I now enter a value and the field would thus be valid, however, the display remains unchanged (red border + error message). If I delete the value again, I get a second error message. This process can be repeated as often as desired and each time a new error message is added. With the second form, which is initialized with a model, this problem does not exist.

Unfortunately I could not find a suitable way to reset the error messages when switching forms. Neither deleting the error messages (MessageStore), nor deleting the attached events or further functions from the validation or the EditContext (Validate, Notify(...), MarkAsUnmodified, etc.) brought a success. However, the error messages are cleared and the events are detached.

It seems to have something to do with the FieldState, as far as I have seen it remains and certainly contains the still displayed error message, the rest of the form is reset correctly and then just generates a new error message in addition to the old state. As a workaround, recreating the EditContext completely new was the only solution so far.

ddudda174 avatar Dec 15 '22 22:12 ddudda174

Hi @ddudda174

Thanks for your investigation.

You said as a workaround, recreating the EditContext completely new was the only solution so far. Can you give an example of how you did this?

Many thanks

DanRogers99 avatar Dec 17 '22 09:12 DanRogers99

@DanRogers99 Well i simply set "_myContext = new EditContext(model)" to reset it while changing between the Tabs. Like this line here.

I haven't tested the stuff from another issue (#168) in here which seems related to this problem, but there might be some other solutions instead of setting a complete new EditContext after the view changed.

According to an answer on stackoverflow the better approach would be to clear the messages from the messagestore for a specific field instead of clearing it for everything, which not seems to work properly.

ddudda174 avatar Jan 05 '23 07:01 ddudda174