02
Aug
07

Using the Validators.validateAll() method to validate a form

I recently saw this issue in FlexCoders and filed a bug for the poster. Well, it turns out the bug I filed was a duplicate of an existing bug, and that bug has been fixed (in Flex 3 — codename:Moxie). So, long story longer, here’s how you can use the Validator class’s validateAll() method to validate a form (which may or may not have enabled validators).

Full code (and workaround) after the jump.

This bug was reportedly fixed in build 176281, so if you have an older Flex 3 build, you may need to use the Flex 2 workaround below. Or, you could go to the nightly Flex 3 SDK builds page on the Adobe Labs site and download a build newer than 176281, which is basically anything newer than Thursday July 12, 2007.

<?xml version="1.0" encoding="utf-8"?>
<!-- FLEX 3 SOLUTION -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" creationComplete="initApp()">

    <mx:Script>
        <![CDATA[
            import mx.events.ValidationResultEvent;
            import mx.controls.Alert;
            import mx.validators.Validator;

            private var myValidators:Array;

            private function initApp():void {
                myValidators = [valid1, valid2];
            }

            private function clickHandler():void {
                var errors:Array = Validator.validateAll(myValidators);
                if (errors.length == 0) {
                    Alert.show("Looks valid to me.", "SUCCESS");
                }
            }
        ]]>
    </mx:Script>

    <mx:StringValidator id="valid1" source="{ti1}" property="text" minLength="4" maxLength="6" />
    <mx:StringValidator id="valid2" source="{ti2}" property="text" minLength="4" maxLength="6" enabled="false" />

    <mx:TextInput id="ti1" />
    <mx:TextInput id="ti2" />

    <mx:Button label="validate" click="clickHandler()" />

</mx:Application>

If you are using Flex 2.0.1 (Hotfix 2 or Hotfix 3), the easiest workaroud I’ve found is to just filter the array of validators that you pass to the validateAll() method so that no validators are passed if their enabled property is set to false. Thankfully this is pretty simple if you use the Array class’s filter() method, as seen below.

<?xml version="1.0" encoding="utf-8"?>
<!-- FLEX 2 SOLUTION -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" creationComplete="initApp()">

    <mx:Script>
        <![CDATA[
            import mx.events.ValidationResultEvent;
            import mx.controls.Alert;
            import mx.validators.Validator;

            private var myValidators:Array;

            private function initApp():void {
                myValidators = [valid1, valid2].filter(filterDisabledValidators);
            }

            private function clickHandler():void {
                var errors:Array = Validator.validateAll(myValidators);
                if (errors.length == 0) {
                    Alert.show("Looks valid to me.", "SUCCESS");
                }
            }

            private function filterDisabledValidators(element:*, index:int, arr:Array):Boolean {
                return element.enabled;
            }
        ]]>
    </mx:Script>

    <mx:StringValidator id="valid1" source="{ti1}" property="text" minLength="4" maxLength="6" />
    <mx:StringValidator id="valid2" source="{ti2}" property="text" minLength="4" maxLength="6" enabled="false" />

    <mx:TextInput id="ti1" />
    <mx:TextInput id="ti2" />

    <mx:Button label="validate" click="clickHandler()" />

</mx:Application>

For more information on the bug, and where I got the code for this post, see the bug in the public Flex bugbase at: http://bugs.adobe.com/jira/browse/SDK-9408.

Happy Flexing!


3 Responses to “Using the Validators.validateAll() method to validate a form”


  1. 1 Tony Fendall Aug 2nd, 2007 at 12:43 pm

    You post a lot of great examples, and I’m really greatful, but it would be cool if you could get a plugin to put syntax highlighting on the code snippets.

    http://codex.wordpress.org/Plugins/Syntax_Highlighting

  2. 2 peterd Aug 2nd, 2007 at 12:51 pm

    Tony,

    Yeah, I’ve been thinking about doing that. Usually I try and link to the MXML so you can see the raw files, as well as do View-Source enabled SWFs, but it all depends on if I have time or not (I write either before work or late at night).

    Can you recommend a good syntax highlight plugin from the list?
    I like WP-Syntax, but the plugin doesnt seem to work very well with my Redoable theme.

    Peter

  3. 3 Borek Aug 3rd, 2007 at 12:07 am

    Yes, filtering is probably the easiest workaround I know of.

    Borek

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".