Reputation: 48139
From my reading and trying to understand and implement MVVM and validation, I need some help on the following scenario.
View - User interface (expected)
Model - based on a data table, and via OnColumnChanging, tests for validation requirements on a column-by-column basis (this part works no problem)
ViewModel - glue the connects the model to the view, this works too.
For each of the textbox controls, I have them respectively binding two-way to the data table, current row (still no problem) and it shows the data as expected. The flags including: NotifyOnTargetUpdated, ValidatesOnDataErrors, ValidatesOnExceptions and NotifyOnValidationError are all set to true.
If I use the interface and put in invalid value, it properly triggers the validation and puts red border around control showing it failed.
Now the problem. I start my form, and click the "Add" button (new record, blank values), textbox controls now enabled for editing content. Click the "Save" button. I want to have all the controls refreshed that are missing "required" data. Since my view model is bound to columns of the data table, and that from my readings, all the validation should be handled in the viewmodel, how should I adjust my scenario.
I don't know how to force which controls are bound to respective controls when they've never received focus. In addition, from other readings, to allow for unit testing, you should be able to test under the assumption that there is never a user interface. So, in theory, I could automate creating my viewmodel, which loads my data model, adds a record, tries to save and forces whatever to test all "required" fields.
Am I close??? way off??? Not quite positive on this.
Upvotes: 1
Views: 322
Reputation: 62248
Validation should be done in two places. One in ModelView
(User input validation), second on Model
model consistency validation, if required for specific scenario.
You have every text box (TextProperty
) is bound to the ModelView's property. TextBox
(I presume) has a dependency property which lets to you specify ether to signal error on UI
or not.
What you have to do, IMHO, immidiately inside Add
event handler set default values to binded ModelView
object. What will happen is: for every control, including required one, will be settuped, thow via DataBinding
visualized on UI
, default value. For required fields the default value can be just not valid one, so requierd field will be immediately appear with Error
signal, signaling a user about presence of mandatory fields.
Upvotes: 0
Reputation: 8786
Implement: IDataErrorInfo
and check Error
within your SaveCommand
Further Reading:
Upvotes: 1