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!

4 thoughts on “Using the Validators.validateAll() method to validate a form

  1. 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

  2. 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

Comments are closed.