Removing an error flag from a form field in Flex

Today I ran into a situation where I needed to clear the error flag from a form field in Flex. My code uses a datagrid to present a list of items to edit. You could click to edit an item, or click an Add button to add a new item.

I ran into a problem though. If I clicked to edit a field, hit cancel, and then hit Add, I was seeing the values from the previous edit in the form field. Well that was easy enough. I added this to my cancel operation:

username.text = ''; password.text = ''; firstname.text = ''; lastname.text = ''; email.text = '';

This worked, but when I returned to add an item, the form was displaying red borders around each field. My code to set the form items to empty strings was also triggering the form validation.

I did a bit of looking around in the API and couldn't find a simple way to "reset" the validators I had applied to my form. Turns out I found a solution on Adobe: Flex Quick Starts: Validating Data.

Turns out you can manually change a property on the text field. If you set errorString to an empty string, the red "You messed up" border goes away:

username.text = ''; username.errorString = ''; password.text = ''; password.errorString = ''; firstname.text = ''; firstname.errorString = ''; lastname.text = ''; lastname.errorString = ''; email.text = ''; email.errorString = '';

Feels a little hackish to me. Anyone know a nicer way?

Archived Comments

Comment 1 by todd posted on 2/7/2007 at 3:06 AM

You could always disable the validator (see http://livedocs.macromedia....

Though you'd have to re-enable it afterwards to get your validation back.

Comment 2 by David Buhler posted on 2/7/2007 at 3:22 AM

theValidator.enabled = false;
theField.text = '';
theValidator.enabled = true;

the ErrorString tends to burn the border which acts as the error flag if it is called too often with errorString='';