Multiple validators on a single component

Flex Published on November 29, 2009 by Andrea Bresolin 8 Comments »
Live demoSource code (LICENSE)

Sometimes, while using validators in Flex, i needed to have more than one validator connected to a single component, e.g. a TextInput. So, at the beginnig, I tried the naive approach, just create multiple validators and assign to all of them the same source and the same property to validate. After a few tests, I quickly realized that in Flex, a single component is meant to have only one validator associated to it. This is a problem in some situations because I have different validatiors that already do what I need and I just want my component to be considered valid only when all the validators are valid. So I investigated more in depth taking a look at the Flex SDK source code and I realized that it’s by design that I simply cannot do it easily. What usually happens is that it’s the last triggered validator that decides if a component is valid or not. We know that everything is based on events dispatching in Flex and that we cannot know the order in which events reach a particular component, so we also don’t know what will be the last validator to be triggered for a component. This is especially evident when a form is quite complex and contains a lot of fields that have to be validated.

What can we do to solve this problem? Here I’m going to explain what I did about a couple of years ago, when I faced this problem. There are rumors that there should be something available in the next version of the Flex SDK (version 4) that solves the problem, but meanwhile, we can still solve it using the manager class I developed. I’ve been using it up until now and it always worked as expected without problems. Basically, the class takes care of all the validation events coming from different validators and makes sure that the right validity state is preserved for the component that has been validated. It doesn’t matter when the events are dispatched, it’s not important, the final result is:

  • a component is considered VALID if all the validators say that it’s valid;
  • a component is considered INVALID if at least one of the validators says that it’s not valid.

Let’s take a look at the source code now. Read the rest of this entry »

WP Theme & Icons by N.Design Studio
©2009-2010 Andrea Bresolin. All rights reserved. - Privacy Policy
Entries RSS Comments RSS