Validatoring Forms in Flex 2
Validating forms are not my favorite thing to do in Flex 2. A lot of the process seems quite tedious. It is a big improvement over Flex 1.5, but typically you have to create the validators and then create a function to see if all the validators are valid/invalid.
It seems the simple approach to validation would be to have the Form be aware of the Validators and provide and easy Bindable way of providing the state of validity for the Form.
Typical Approach (One Validator):
- Create Form
- Create Validator
- Add valid/invalid event listeners
- Create valid/invalid method handler that sets the button.enabled property
Typical Approach (More than One Validator):
- Create Form
- Create Validator
- Add Bindable isValid flag
- Add different valid/invalid event listeners for each Validator or pass a identifier along in the generic function.
- Create different valid/invalid method handler and set a Bindable isValid flag variable. Or create a generic function with a lot of switch cases to set the event’s proper valid and then check if all flags are valid.
- Bind isValid flag to Submit button’s enabled property
Simple Approach:
- Create Form
- Create Validators
- Bind Form.isValid property to Submit button’s enabled property
This is exactly what the AdvancedForm component provides. You still have full control of the Validators, but it provides a simple approach to constraining Form submission.
You’ll find the AdvancedForm component on the Adobe Flex Exchange or under the components folder on my site.