This is a migrated thread and some comments may be shown as answers.

Premature Validation on insert

3 Answers 81 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ben
Top achievements
Rank 1
Ben asked on 16 May 2017, 05:47 AM

I have a grid bound to a list of a view model class I will call "A". Class A implements INotifyDataErrorInfo. As each property of A is set, the value of the changed property is validated, the set of errors is updated, and the ErrorChanged event is fired for that property--this is a pretty typical class that implements INotifyDataErrorInfo.

When the user clicks the New Item Row, I set some initial property values in the AddingNewDataItem event. Then when the new data row displays before the user has begun to type in the first field, a few fields are already highlighted with errors. After adding some logging, I found that the data grid is making a copy of the object I create in the AddingNewDataItem handler. Each property in this copy is set which causes validation to fire off, and some fields show errors when initially shown to the user. I don't want this. I only want to show errors after the user edits the field or attempts to commit a row with errors.

I also noticed the grid control calls HasErrors() on the original source object made in the event handler, not the copy it makes, which seems odd to me.

Is this the expected behavior? I want to display a newly inserted row with no errors and some custom initial values, but that copy the grid makes sorta ruins the process. How do I work around this?

Below is my log. It begins with the hash code of the ViewModel

// NOTE: All this occurs on insert, no user edits
// These first validation calls are in the AddingNewDataItem event when I initialize some properties.
57073170: ValidateProperty JobId, 0 errors.
57073170: ValidateProperty Quantity, 0 errors.
57073170: ValidateProperty State, 0 errors.
57073170: ValidateProperty Part, 0 errors.
57073170: ValidateProperty PartId, 1 errors.
57073170: ClearErrors() //clear errors so that the fields don't indicate errors until the user sets them or the row is validated
// Done with AddingNewDataItem event handler in my code
 
57073170: HasErrors() returned False
57073170: HasErrors() returned False
57073170: HasErrors() returned False
57073170: GetErrors(State) returned empty list
57073170: HasErrors() returned False
57073170: HasErrors() returned False
57073170: GetErrors(State) returned empty list
57073170: GetErrors(PartNumber) returned empty list
57073170: GetErrors(Quantity) returned empty list
57073170: GetErrors(Length) returned empty list
57073170: GetErrors(ComponentType) returned empty list
57073170: GetErrors(Label) returned empty list
57073170: GetErrors(ElevationId) returned empty list
57073170: HasErrors() returned False
57073170: HasErrors() returned False
57073170: HasErrors() returned False
57073170: HasErrors() returned False
 
//NOTE: looks like new object is now created by RadGridView after this point?
13218022: ValidateProperty PartId, 1 errors.
13218022: ValidateProperty PartNumber, 0 errors.
13218022: ValidateProperty Description, 0 errors.
13218022: ValidateProperty Quantity, 0 errors.
13218022: ValidateProperty ComponentType, 0 errors.
13218022: ValidateProperty Label, 1 errors. // This error shows in the UI clicking Add Item Row
13218022: ValidateProperty ElevationId, 1 errors. // This error shows in the UI after clicking Add Item Row
13218022: ValidateProperty JobId, 0 errors.
13218022: ValidateProperty KeepAlways, 0 errors.
13218022: ValidateProperty Length, 0 errors.
13218022: ValidateProperty Part, 0 errors.
13218022: ValidateProperty PartId, 1 errors.
13218022: ValidateProperty Id, 0 errors.
13218022: ValidateProperty State, 0 errors.
 
// HasErrors is called on the original and not the new object...
57073170: HasErrors() returned False
57073170: HasErrors() returned False
57073170: HasErrors() returned False
57073170: HasErrors() returned False
57073170: HasErrors() returned False
57073170: GetErrors(PartNumber) returned empty list
57073170: HasErrors() returned False
57073170: HasErrors() returned False
57073170: GetErrors(PartNumber) returned empty list
57073170: GetErrors(PartNumber) returned empty list
57073170: GetErrors(PartNumber) returned empty list

 

3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 18 May 2017, 03:03 PM
Hello Ben,

Thank you for the detailed explanation of the scenario.

If I am understanding your requirement correctly, you can benefit from the ValidatesOnDataErrors property of RadGridView. If you set it to InEditMode, the validation logic will be triggered after the user tries to commit an edit. Can you please give it a try? Is this the behavior of the control you are aiming at?

More information can be found in the Validating topic.

Hopefully, this helps.

All the best,
Stefan X1
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Ben
Top achievements
Rank 1
answered on 18 May 2017, 03:54 PM

Thanks for the reply, Stafan,

Yes, that is exactly the sort of thing I was looking for. The only goal I had was to disable validation for cells of a new row until the user modifies the value. It might be beneficial to also see errors in existing rows that are just being viewed and that already existed on the ItemSource when the items are initially loaded. That way, if there is a bug that allows an invalid row to be shown, we would still see the validation error on it. However, that is a minor issue. The property you mentioned makes the validation work good enough for our needs.

 

Thanks

0
Stefan
Telerik team
answered on 23 May 2017, 01:09 PM
Hi Ben,

Thank you for this additional clarification.

I am happy that using the ValidatesOnDataErrors property suits your needs.

In case you need any other assistance with our components, do not hesitate to contact us again.

All the best,
Stefan X1
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Ben
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Ben
Top achievements
Rank 1
Share this question
or