1 Reply Latest reply on Aug 6, 2011 7:51 AM by Amy Blankenship

    Validating an array of "items"


      I'm looking for some ideas, suggestions, experiences, etc. related to how best to handle a scenario such as this...


      I have Flex app where I create an array of "items" each of which is an instance of the same ActionScript class. The instances each have several properties/data values. The properties of only one item is displayed in the user interface at a time, but the user can switch between the items as they edit the values that make up each item. Most of the values/properties ultimately need to be validated in one way or another prior to committing all the "items" to a database.


      So, prior to commiting the array of items to a database I need to validate each "item" in the array. I suppoes I'll need an array of various validators for each item which I can then call validateAll on. If validation failures occur I plan on displaying the combined list of errors in a separate dialog indicating which item failed which validation. And I suppose that for the item which is currently being displayed in the UI I could also set the "listener" on the validators to be the UI items that contain the values being validated so this items would be highlighted in the UI.


      But, am I going down the right path here?  Is there a different better way to do this sort of thing? Should I even be using Flex validators in this situation?



        • 1. Re: Validating an array of "items"
          Amy Blankenship Level 4

          One way you could do this is to make validation part of the object.  If you use getter/setter pairs for your properties, you can validate each time the values change.  You can then make the validity "state" available as a property (or properties) of the object.


          You could also create a "companion" object that watches each object that needs to be validated (i.e. the validator object has a reference to the object it is watching, but the observed object doesn't know anything about the companion object).


          In either case, I'd say you will probably want to use the Flex validators internally to the component doing the validating.  Keep in mind you can use validators in Actionscript as well as MXML.

          1 person found this helpful