Validating items

Topics: Developer Forum
Mar 13, 2009 at 8:18 AM
Hi. I need to implement some validation to my item's property before it will be save. I see on the forum about Context.Current.Persister.ItemSaving += ...
So, my question is: Where i should insert this code to implement validation in N2 admin-edit page? And what type of exception i need to trowns?
Mar 13, 2009 at 11:03 AM
If your validation logic can be expressed via regular expressions, that you might just specify it as a named argument to [Editable*] attribute over poroperty in question.

On the other hand, if you need a more complex validation rules, than i bet you should implement a custom [Editable*] attribute. (That is: you derive from the most suited one, and then in the overriden AddEditor method you just setup your validator in code. You have to be a little bit familiar with a custom web controls though..). You may also integrate validation logic into custom web control and then use that control as an editor for [Editable*] attribute. Unfortunately, my example is not that easy: a SelectUser control uses MembershipUserValidator and then they both are applied at the line 36 via standard [Editable] attribute.