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!

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

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

  1. Tony Fendall says:

    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. peterd says:

    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. Borek says:

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

    Borek

  4. 1. TextInput creates dynamically

    textInputBox = new MyTextInput;

    textInputBox.restrict = “0-9.”;
    textInputBox.maxChars = 24;
    amountValidator = new NumberValidator();
    amountValidator.source = textInputBox;
    amountValidator.property = “text”;
    amountValidator.allowNegative = false;
    amountValidator.domain = “real”;
    amountValidator.precision = 4;
    amountValidator.required = false;
    amountValidator.maxValue = 999999999999.9999;
    amountValidator.trigger = textInputBox;
    amountValidator.triggerEvent = Event.CHANGE;
    amountValidator.addEventListener(ValidationResultEvent.VALID, amountValid);
    amountValidator.addEventListener(ValidationResultEvent.INVALID, amountInvalid);

    private function amountValid(event:ValidationResultEvent):void
    {
    valid = true;
    fieldsValidated = true;
    }

    private function amountInvalid(event:ValidationResultEvent):void
    {

    valid = false;
    fieldsValidated = true;
    }

    2. As mention in the creation, when we exceed the limit, it shows error my red color border, and the same time if you delete them by DEL key when it come to the given acceptable limit, automatically become to green soon.
    3. Leave from the field and change values of another textinput(this is just a textinput, this is a form there are some more form elemets), then come back to value exceeded textfield by SHIFT+TABS and remove the additional entered numbers, when you come to green soon your value is accepted.
    4.Now again enter more values and now you are in the warn zone, then leave from the field and do the few changes in other form elements.
    5. Then come back to the value exceeded text filed by MOUSE CLICK, and start delete from DEL, even though you removed additional values, still fields shows that you are in warn zone.

    Actual Results:
    Even when remove additional numbers,still field is Red

    Expected Results:
    if remove additional numbers, field should come its normal status.

    Picture of this issue can be viewed at http://bugs.adobe.com/jira/browse/SDK-24372

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree