This is a migrated thread and some comments may be shown as answers.

Validation and InvalidState

1 Answer 97 Views
Input
This is a migrated thread and some comments may be shown as answers.
Jerome
Top achievements
Rank 1
Jerome asked on 01 Dec 2011, 11:29 PM
I'm trying to find the proper way to deal with Validation, either using RequiredFieldValidator, RegularExpressionValidator, or a CustomValidator. When one of these validators hooked up to a Telerik RadInput control fails, I'd like the control to switch to it's Invalid State, showing the warning, etc. I can't find anyway to do this. 

[EDIT]

I've managed to figure out how to use RadInputManager to validate some of my textboxes... except that it doesn't seem to be invoked during Page.Validate(group), even if I set a group on the TextSetting. It seems like the TextSetting isn't actually added to the Control hierarchy, and so is never located. It also only has a single ErrorMessage property, which makes it impossible to actually specify different message for different inputs, even if their behaviors are the same: First name is required, last name is required, foo is required, blah is required, etc.

So other than setting the Invalid state of the controls... I can't figure out how this makes things EASIER?

1 Answer, 1 is accepted

Sort by
0
Jerome
Top achievements
Rank 1
answered on 02 Dec 2011, 05:58 PM
Solved this, idiotically. This begs for a cleaner solution inside RadInputControl.

I've created a class, RadInputValidationDecorator, which inherits from Control. It internally creates a CustomValidator subclass, which invokes a JS function on validation. This validation function is always valid. Instead, it loops over the target controls' Validators array, and checks isvalid on each object. If any are invalid, it sets ._invalid to true on the RadInput, and then .updateCssClasses(). This causes the appearence change I'm looking for. It also does the same on the server side, on PreRender, it checks whether all associated Validators (other than itself) with the RadInput are valid, if not, it injects some JS which sets _invalid on the client side when the page is first loaded.

My solution relies on the Decorator being created and added to the Controls collection AFTER all other validators for the control, otherwise it's validation logic will be fired before the other validators have had a chance to set their 'isvalid' value. This is kind of hacky, but I can't figure out any other way to inject myself into the client side validation logic. 

I guess I could have javascript that resorts Page_Validators and control.Validators on load. Sounds even more hacky, but is probably the only real solution.

This is hacky and lame. A RadInput control should be smart enough to change it's own state to invalid when a validator hooked up to it fails. What I ended up doing is something the RadInput itself could do, perhaps by adding itself to it's own Validators collection, to participate in Page validation, or something.

Anyways, problem sort of solved.
Tags
Input
Asked by
Jerome
Top achievements
Rank 1
Answers by
Jerome
Top achievements
Rank 1
Share this question
or