c# - MVVM - Validating the model when a field in the viewmodel is changed -


i'm using mvvmlight libraries database-first ef model. can't quite figure out how validate model when field in viewmodel has changed. works fine when i'm updating individual properties of model have call validate in set methods. i'm not sure how works elsewhere.

for sake of example, have person class (model) in application has property called name implemented in view model:

private person _currentperson;   // code omitted...  [required(errormessage = "name required field.")] public string name {     { return _currentperson.name; }     set     {         if (value != _currentperson.name)         {             _currentperson.name = value;             raisepropertychanged();             validate(_currentperson.name);         }     } } 

when want create new person, have method attached relaycommand called newperson code:

public relaycommand newcommand { { return _newcommand ?? (_newcommand = new relaycommand(newperson)); } }  // code omitted...  private void newperson() {     _currentperson = new person();     raisepropertychanged(string.empty); // updates model , ui. } 

how validate _currentperson without calling validate on every single property? because model class has quite few properties... (30+).

the thing i've tried using reflection iterate through properties of person object, kept causing exceptions didn't understand.

update: managed working, there has better way of doing this:

private void newperson() {     _currentperson = new person();     raisepropertychanged(string.empty); // updates model , ui.     validateperson(_currentperson); }  private void validateperson(person p) {     validate(p.forename, "forename");     validate(p.surname, "surname");     validate(p.dateofbirth, "dateofbirth");     // there's 30 calls validate here... } 

how validating whole person object in validate() methode, not single property?


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -