views:

1227

answers:

1

Hi there,

I'm developing a SL3 application with Prism. I need to have support for validation (both field level (on the setter of the bound property) and before save (form level)), including a validation summary, shown when the save button is pressed.

But the samples I can find googling are either SL3 with a lot of code in code behind (very uncool and un-Prismy), or WPF related.

Does anyone know a reference application with some actual validation I can look into?

Cheers, Ali

+2  A: 

There aren't any from Microsoft at present, but I'll pass this one onto the PRISM team tomorrow to see if we can get a basic Form Validation example inside the next rev of PRISM.

That being said, you can put a validator per Form that essentially validates each field (semantic and/or syntax validation) and should all pass, will return a true/false state.

A way I typically do this is I attach a "CanSave" method to my Commands ie:

SaveOrderCommand = new DelegateCommand<object>(this.Save, this.CanSave);

private bool CanSave(object arg)
{
     return this.errors.Count == 0 && this.Quantity > 0;
}

Then in the this.CanSave, i then put either the basic validation inside this codebase, or I call a bunch of other validators depending on the context - some would be shared across all modules (ie IsEmailValid would be one Validator i place in my Infrastructure Module as a singleton and pass in my string, it would then true/false as a result). Once they all pass, ensure CanSave returns true. If they fail, the CanSave will return False.

Now if they fail and you want to trigger a friendly reminder to the user that its failed theres a number of techniques you can use here. I've typically flagged the said control at validation as being "failed".. (i wrote my own mind you, so up to you which toolkits you could use here - http://www.codeplex.com/SilverlightValidator is a not bad one).

Now, I typically like to do more with Forms that have validation on them by not only highlighting the said control (red box, icon etc) but also explain to the user in more detail whats required of them - thus custom approach is a solution I've opted for.

At the end of the day, you're going to have to do some of the heavy lifting to validate your particular form - but look into ways to re-use validators where they make sense (email, SSN etc are easy ones to re-use).

HTH?

Scott Barnes - Rich Platforms Product Manager - Microsoft.

Scott Barnes