Flex Tip - Trigger validation when submitting form
I just noticed a common pattern in the way I do validation, so I thought I would share. (in case you didn't know this already)
The validators are great at telling the user what is required or failing validation as the tab through the form. However they don't actually block the user from submitting the invalid form. But there is a simple way to do this with the validators.
1. Group all of the validators into an MX:ARRAY tag, and give it a unique id. - I usually use "validators"
<mx:StringValidator source="{abbreviations}" property="text" required="true"/>
<mx:StringValidator source="{subcode}" property="text" required="true"/>
<mx:StringValidator source="{name}" property="text" required="true"/>
<mx:StringValidator source="{letterType}" property="text" required="true"/>
<mx:StringValidator source="{scheduleType}" property="text" required="true"/>
</mx:Array>
Since the validators are now all grouped in an Array, you can call the Validator.validateAll() function to check them all at once.
2. Use this if statement in the click event of your form submit button to check the validators before submitting. You can also add this if check inside your doSave() method too. whichever you prefer.
<mx:Button
label="Save"
click="if( Validator.validateAll(validators).length==0 ){ doSave() }"/>

Cheers,
Oliver