Forms… Any data-centric application is going to have its fair share of them. They are fairly tedious work. Layout out the form, manage the output, validate the output, over and over. Validation is best when it is active, to let the user know that they have made a mistake before they try to submit the data. Optimally the user won’t be allowed to continue until the form data is complete and accurate.
Every form I’ve written carries the same structure for validation, so as a dedicated lazy programmer I wrote a simple FormValidator class to handle the boilerplate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package com.visualempathy.validators { import flash.display.DisplayObject; import flash.events.EventDispatcher; import flash.events.IEventDispatcher; import mx.events.ValidationResultEvent; import mx.validators.Validator; [Bindable] public class FormValidator extends EventDispatcher { public var formIsValid:Boolean = false; public var validators:Array; private var focusedFormControl:DisplayObject; public function FormValidator(target:IEventDispatcher=null) { super(target); } public function validateForm(event:Event):void { focusedFormControl = event.target as DisplayObject; formIsValid = true; for each( var validator:Validator in validators ) validate(validator); } private function validate(validator:Validator):Boolean { var validatorSource:DisplayObject = validator.source as DisplayObject; var supressEvents:Boolean = validatorSource != focusedFormControl; var event:ValidationResultEvent = validator.validate(null, supressEvents) var currentControlIsValid:Boolean = event.type == ValidationResultEvent.VALID; formIsValid = formIsValid && currentControlIsValid; return currentControlIsValid; } } } |
in the MXML I add an Array structure to hold the Validators, and add the FormValidator like so:
1 2 3 4 5 6 7 8 9 10 | <mx:Array id="validators"> <mx:StringValidator id="studioNameValidator" source="{this.studioNameInput}" property="text" required="true"/> <mx:EmailValidator id="emailValidator" source="{this.emailInput}" property="text" required="true"/> <mx:PhoneNumberValidator id="phoneValidator" source="{this.phoneInput}" property="text" required="true"/> <mx:StringValidator id="addValidator" source="{this.addressInput}" property="text" required="true"/> <mx:StringValidator id="cityValidator" source="{this.cityInput}" property="text" required="true"/> <mx:StringValidator id="stateValidator" source="{this.stateInput}" minLength="2" maxLength="2" property="text" required="true"/> <mx:ZipCodeValidator id="zipcodeValidator" source="{this.zipcodeInput}" property="text" required="true"/> </mx:Array> <validators:FormValidator id="formValidator" validators="{this.validators}"/> |
This allows you to add an arbitrary number of validators. Then in the various inputs:
1 | <mx:TextInput id="studioNameInput" text="{this.studio.name}" width="100%" change="this.formValidator.validateForm(event);"/> |
with the button to commit the changes enabled only when the form is valid:
1 | <mx:Button id="saveChangesBtn" label="save" click="handleSaveChanges()" enabled="{this.formValidator.formIsValid}"/> |
It reduces some of the pain and tedium with the process of creating forms, which is always welcome.
The Form Validation for the Lazy Programmer in Flex by Joel Hooks, unless otherwise expressly stated, is licensed under a Creative Commons Attribution 3.0 United States License.