Package mycompenent
{
Import MX. validators. validator; // reference the validator class.
Import MX. validators. validationresult; // reference the validationresult class.
Public class myvalidators extends validator
{
Public Function myvalidators () // Constructor
{
Super ();
}
Private var results: array; // defines an array to store errors.
// Rewrite the verification function
Override protected function dovalidation (value: Object): Array
{
VaR S: String = value as string;
Results = []; // clear the Array
Results = super. dovalidation (value); // verify it by using the dovalida tion Method in the inheritance class.
If (results. length> 0) // if an error occurs during verification, the error message is returned.
Return results;
If (S. length> 6) // custom verification. The character length cannot exceed 6
{
// Record error information
Results. Push (New validationresult (true, "text", "stringtoolong", "the character length exceeds 6 "));
}
Return results;
}
}
}
(5) The extends keyword indicates inheritance.
(6) The Super keyword indicates that the method in the subclass is inherited. Therefore, the "super. dovalidation (value)" statement indicates that the dovalidator method in the subclass is inherited.
(7) The override keyword indicates the rewrite function.
(8) The validationresult class is the verification result class, which is included in "MX. validators. The constructor of the validationresult class has four parameters. The first parameter indicates whether the error type is used, the second parameter indicates a specific attribute, the third parameter indicates the error type, and the fourth parameter indicates the error prompt.
(9) The "stringtoolong" error type is customized in this program, and the message "the character length exceeds 6" is displayed ". This error occurs when the data component length exceeds 6 characters.
(4) Call the custom verification class.
You must specify the namespace when calling a custom component. The components provided by Flex 3.0 are all under the MX namespace, so the xmlns: MX = "http://www.adobe.com/2006/mxml" statement is automatically generated after the mxml is created.
The syntax for referencing a custom component is as follows.
<Outermost component... Xmlns: space name = "package to which the class belongs">
...
<Space name: Custom component/>
The space name can be any name. The package keyword of the class must be the same as that of the class definition.
The following code references the custom component myvalidators.
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml"
Xmlns: mycomp = "mycompenent. *">
...
<Mycomp: myvalidator... />
The following code calls the myvalidator component to verify the data in the input box.
<? XML version = "1.0"?>
<Mx: applicationxmlns: MX = "http://www.adobe.com/2006/mxml" xmlns: mycomp = "mycompenent. *" fontsize = "13">
<! -- Custom component myvalidators, used to verify string data -->
<Mycomp: myvalidators id = "myval"
Source = "{txtstring}" property = "text"
/>
<Mx: paneltitle = "Custom verification component" width = "300" Height = "200" horizontalalign = "center" verticalalign = "Middle">
<Mx: textinput id = "txtstring"/> <! -- Input box component -->
<Mx: Label text = "{txtstring. errorstring}"/> <! -- Label component, used to display verification results -->
<Mx: button label = "verify" Click = "myval. Validate ();"/> <! -- Button component to trigger verification -->
</MX: Panel>
</MX: Application>