The validators that are provided in Flex cannot be localized or have the error strings easily changed because they cannot have an id, which means they cannot be accessed from script and you cant bind objects that dont have an id. One solution is to subclass the various validators and override their error string properties so they access variables in the application. Then all you have to do is load in strings into those variables and youre all set.
In the example, if you type xyz and hit tab, you will see the validator kick in with the custom message from the app, that calls the invalidCharError() and in the AS file you will see that it returns the string defined in the MXML file. You can also type in a custom error in the second TextInput and change the error on the fly allowing you to localize the errors easily from an XML file or other source. Also try typing in 123 and hitting tab on the first input and you will see another customized error message. Just locate the different methods in the MXML AS API and override each of the methods you need custom errors for.Note that the validation logic itself doesnt change at all you are just overriding the error messages.
Thanks to Alex for providing a starting point for this example. You can download the code here and it is also shown below
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" >
var invalidCharError:String =
"Default Error for Invalid Phone Number!!";
var wrongLengthError:String =
"Hey dummy the phone number needs to be at least 10 digits long!!";
<mx:Label text="Phone Number"/>
<mx:TextInput id="phone" />
<mx:Label text="Custom Error Message"/>
<mx:Button click="invalidCharError = newError.text"
label="Change Error Message" />
Subclassed Validator (MyPhoneNumberValidator.as):
// Simple age validator.
// Verifies: AS components
class MyPhoneNumberValidator extends mx.validators.PhoneNumberValidator
// The default is "Invalid characters in your phone number."
public function get invalidCharError():String
// The default is "Your telephone number must be at least 10 digits in length."
public function get wrongLengthError():String
After I posted this I realized that Matt Chotin had an answer for this as well. http://www.markme.com/mchotin/archives/006848.cfm
This post is throwing errors on your RSS feed. I tend to browse your blog through an Aggregator so I'd realy appreciate if you could fix this line:-
Then all you have to do is load in strings into those variables and you're all set.
You need to fix the apostraphe in you're.