tags:

views:

163

answers:

1

I listened to this herding code podcast on MVC, MVP, MVVM yesterday and was struck by the idea of sending your whole ViewModel object to a validator which does nothing but validate all the fields in it and send it back.

  • has anyone implemented that type of validation pattern?
  • how did it look technically?

I am thinking of extending this idea by also having a "FormPreparer" which receives the whole ViewModel after the Model data, field metadata, and other user and context data is fed into it, then this "FormPreparer" prepares all the fields on the form which will be on the View so that e.g.

  • date fields are represented by DatePicker controls
  • e-mail fields are represented by textBoxes with e-mail validation
  • and e.g. the Customer field is a dropdown of customers

the metadata defines these things about each field:

  • type (text, date, date/time, duration, email, url, customer)
  • control (textbox, multiline textbox, dropdown, radiobuttons, checkbox, clickbutton)
  • label (e.g. "First Name")
  • helptext (e.g. "this is the number you find on the top of Form 4A")
  • example ("#123ABCD")
  • display tab (e.g. for forms that consist of a number of tab areas)
  • display area (e.g. for forms that group fields into areas)
  • display order (e.g. the order of the fields in the group)
  • value (e.g. "Jim")
  • autosuggest data (an array of names which needs to be displayed when the user begins to type)
  • field status (readonly, edit, hide)

the "FormPreparer" would combine all this information and then present data to the View which:

  • shows all form data in appropriate controls (dates as datepickers, descriptions in multiline textboxes, etc.)
  • takes care of all validation automatically
  • would only display fields which the current user is allowed to see and would only let him edit data which he is allowed to edit
  • etc.

Has anyone programmed a WPF/MVVM application along these lines?

A: 

No. I'm working on a WPF/MVVM project, but we have not taken such a generic approach to validation. We are creating a custom validation method in each view model with validation logic specific to each view model.

A generic validation routine that could be used for all view models would be great.

Jim Anderson