Need Flex Migration Help?
- Adobe ends Flash player support in 2020
- Flex apps will no longer function
- We can help migrate your app
Ever have an MXML page with a bunch of validators that need to be checked before the page can be submitted?
Here is quick trick to keep yourself a little more sane.
When the submit button is clicked, call a method which loops through this array and returns a cumulative status.
import mx.events.ValidationResultEvent;
protected function checkValid():Boolean {
var vResult:ValidationResultEvent;
var valid:Boolean = true;
if ( validators ) {
for ( var i:int=0; i
protected function handleSubmitBtn( event:Event ):void {
if ( checkValid() ) {
//Do something important with your data }
}
It just so happens that Adobe has a method that does this looping for you. It is a static method of the validator class, Validator.validateAll(). It basically does the same thing as the loop above (with more error checking), but it returns an array as opposed to a boolean. In that case, your final method could look more like this:
protected function handleSubmitBtn( event:Event ):void {
if ( Validator.validateAll(validators).length == 0 ) {
//Do something important with your data }
}
Or you may choose to still buffer it with another method and keep the interface to a simple boolean. That's up to you. Either way, this isn't terribly innovative, but it might save you a few cycles and some headaches while providing a cleaner way to deal with these situations. ML
You know I’m pretty sure there is a ValidateAll() method that does exactly what you’ve done here. Basically checking an array of validators and if everything is valid, it returns null, or at the very least true.
It almost does. ValidateAll does loop through an array of validators, but it returns an array of ValidationResultEvents… so, you could also just pass this array to that method and check the length. The more interesting part is that the array is declared implicityly in MXML. I will add the validateAll part though to the post.