.NET Custom validators – perfect tool (almost)
I don’t know about you, but working with .NET validators is always a fiddly job. Sure, they enable you to do quick validations, but I am yet to find an app where each field only needs one validator. And this is where things usually get interesting.
Using Display=”Static” on all validators messes your display, as static validators are only hidden for display. Using Display=”Dynamic” messes your ValidationSummary control output functionality. Using Display=”None” is mostly useless as there is no information as to which field has caused validation error. Unless you specify it in an error string, which then confuses your end-user.
Hence, the only suitable solution is a control named CustomValidator. There isn’t a thing you cannot do with it. You can use it to color border of your field instead of displaying Text property. You can use it to validate the most obscure validation, you can think of and all in on validation cycle, etc., etc.
Sounds perfect, right? Well, almost. There is one thing wrong with CustomValidator control, which, for me, is almost a show stopper. CustomValidator controls do not fire their ServerValidate events when control is empty (WTF?), leaving you in a conundrum you wanted to circumvent in the first place: using more than one validator for one field and thus avoiding compromises between functionality and web page outlook.
Leave a Reply