UI Validation for multiple views in a region does not work

Topics: Prism v4 - WPF 4
Mar 8, 2014 at 5:29 AM
Hi,

I am trying to load multiple data entry views onto a single content region. On each View the data context (View Model) is bound to presentation objects. All the presentation objects together form a business object which will get saved to the database on a global command.

The problem here is achieving UI validation where on the global command, the Validation.ErrorTemplate should be visible for all the views on view activation.

Any suggestions will be helpful.

Thanks,
Jeevan
Mar 10, 2014 at 3:27 PM
Hi Jeevan,

Based on my understanding, you would like to validate every View when each one gets active by showing the Validation.ErrorTemplate. Then, you could have the global command to be handled on another Control Region. The Global command would be executed when handling the subscription for the MainRegion's Active View changed.

The separate Control View registered on the Control Region may manage the business object persistence.

If I haven't completely understand the scenario you described, I would ask you for more details about the application flow. Therefore, I would give you better support on your issue.

I hope this helps,

Regards.
Gabriel Ostrowsky.
https://blogs.southworks.net/gostrowsky
Mar 11, 2014 at 1:57 PM
Thanks Gabriel for the the response.

You got it right, I have a Global Command which Invokes a FormValidator component (custom component). This component invokes the IDataErrorInfor.Validate(onActionCompletedCallback) on all the views loaded within the main Content Region including the Active and Inactive Views. The problem is the validation happens fine but the Validation.ErrorTemplate seems to be set only for the Active View and when I navigate to the Inactive Views, though the validation function is executed, I do not see the ErrorTemplates getting applied. There are times when I trigger the global command and the active view also does not get the ErrorTemplate set.

I am not sure if I am missing something or if there is a better way to handle this.

Regards,
Jeevan
Mar 14, 2014 at 6:58 PM
Hi Jeevan,

How would you handle the ErrorTemplate and show it on each View? Are you binding a Property with a two-way mode set?

If you could share a small sample with this particular behavior, I would gratefully debug it to troubleshoot this issue.

Regards.
Gabriel Ostrowsky
https://blogs.southworks.net/gostrowsky
Mar 19, 2014 at 11:48 AM
Hi Gabriel,

I have 2 View classes V1.xaml and V2.xaml both have a corresponding datacontext set as V1VM.cs and V2VM.cs. There is a presentation object Root.cs having two properties Root.Property1 and Root.Property2 where Property1 returns Child1.cs and Property2 returns Child2.cs, both are complex types. Now, in the view models, Child1.cs and Child2.cs are exposed as properties. On Application load, V1, V2, V1VM and V2VM are all loaded to the Mef container. V1 and V2 are registered to the main Content region through AutoPopulateExportedViewBehavior. Onn INavigationAware.OnNavigateFrom(), the view model properties are set from the Root object. In the XAML, the textbox property is set as below;

<TextBox name="txtNoOfStages" Text="{Binding Child1.NoOfStages, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=LostFocus}" Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}" Style="{DynamicResource TextBoxMedium}" />

I am using the MVVMValidator to do the validation. So I implement the IDataErrorInfo and configure the validation rules on the Child1.cs and Child2.cs. On a global save command, my FormValidator component which adds the instance of V1VM and V2VM fires the Validate method on the Child1 and Child2 instance accessible through the view models. Here the validation happens on the presentation objects and the error is only displayed on the current active View on the region. If I now activate the second view model on the same content region, I do not get to see the adorners though the rules get executed.

Let me know if you still need a sample and I can share one.

Regards,
Jeevan
Mar 19, 2014 at 9:10 PM
Hi Jeevan,

I would suggest you to raise the Validation errors again for the inactive Views when navigating into them. Therefore, the Validation errors stored on the inactive Views would be displayed after raising them again on the OnNavigatedTo() method when the View would become active.

If that still does not solve the problem, it would be helpful if you could provide a sample with this behavior.

Regards.
Gabriel Ostrowsky
https://blogs.southworks.net/gostrowsky
Apr 1, 2014 at 3:05 AM
Apologies for this late reply.

I have tried your suggestion earlier but here each time I navigate, I get the adorners. On the first time navigate without initiating any data capture, I should not validate. Only on click of Save I should validate before submit. For this reason, tried creating a bool value in the application context to identify Save click but does look like it helps since resetting the validation is a bigger pain. :(

Any thoughts or suggestions?

Regards,
Jeevan
Apr 4, 2014 at 7:10 PM
Hi Jeevan,

Based on my understanding you have successfully got the adorners every time you navigate to a View.
Therefore, adding a boolean parameter for the Save action should not affect the validation resetting. It may just enable/disable the adorners from being viewed and submitted.

If I haven't understand correctly I would appreciate if you could describe what action would be difficult to implement, and send a sample from which I could get a better understanding of the problem and find a proper solution to it.

Regards.
Gabriel Ostrowsky
https://blogs.southworks.net/gostrowsky