Validating a checkbox in Flex via MXML

29 10 2009

The other day, I ran into some trouble where I needed to validate a checkbox in Flex. I wanted to use a built-in validator, mostly for it’s ease of use, and because I am trying to keep basic elements like that into the MXML where I can. I searched around, and found some great tutorials on using Validators, but nothing on doing it to a checkbox. So I endeavored to figure this out on my own. In the end I used a StringValidator to accomplish this. When it attemts to validate on the checkbox, the “selected” property is automatically coerced into a string format. So in case anyone else wants to do the same thing, use the code below.

<mx:FormItem label="I agree to the terms of use" required="true">
<mx:CheckBox id="terms"/>
</mx:FormItem>
<mx:StringValidator source="{terms}" required="true" property="selected" maxLength="4" requiredFieldError="You must agree to the Terms of Use." tooLongError="You must agree to the Terms of Use."/>

Advertisements