I have recently posted about the ability to use Bean Validation with various presentation technologies (here and there). Now Flex is part of the family. The good guys at GraniteDS have added support for Bean Validation into their project and hence Bean Validation is usable by all Flex users.
The work was not as straightforward as for JSF, Wicket or other Java technologies. Because they do not run on the JVM, they basically have reimplemented the full specification in Flex:
- you can annotate your ActionScript3 objects with constraints: it supports all the standard constraints and you can write your own constraints
- you can execute the validation logic and retrieve the error report
- you can make use of most of the feature including advanced ones like groups, group sequence etc
And just like we did with JSF 2 and Bean Validation, the Flex forms do transparently call the constraints based on your domain model. What's also interesting for GraniteDS users keeping their Java domain model and ActionScript3 domain model in sync via Gas3, the constraints are kept in sync.
There are a couple of gotchas to be aware of. The port is not a one-to-one copy: some interfaces or design choice did not make sense due to how ActionScript3 works but overall this is a very close port
- the constraint implementation is in the same class as the constraint declaration (not a problem in a dynamic language)
- @Pattern has a sightly different semantic because the regexp engine in Flex is a bit different.
- instead of the features provided by ConstraintValidatorContext, you can define a properties attribute in your constraints to make it belong to several sub-properties. not as flexible but good enough in many cases.
- and maybe a few other things
Anyways, I won't talk to Franck until he has a 100% compliant implementation of the spec ;) Actually, I've got one real RFE: the documentation for this new validation module is part of the 'Advanced AS3 Features' chapter. I think this feature is simple enough and useful enough to warrant a better place.
To get the full documentation, check it out here. This is all part of the upcoming 2.2 version of GraniteDS.
PS: GraniteDS 2.2 also provides a full implementation of arbitrary-precision numbers, something that is surprisingly lacking from the Flex platform. So if you use Flex and are in banks or any other number crunching industry, check out their BigInteger and BigDecimal implementation.