mx.validators.Validator的错误提示文字的样式修改
mx.validators.Validator默认的错误提示显示英文文字的确很好看,但显示中文就效果太差了,小的看不清,而且是加粗的。由于mx.validators.Validator没有直接提供应用Style的方法属性,下面的方法就是用StyleManager修改错误显示效果:
test.mxml文件内容:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
xmlns="*" verticalGap="0">
<mx:Script>
<![CDATA[
import mx.styles.StyleManager;
function changeNormal() {
StyleManager.styles.ErrorTip.fontWeight = "nomal";
StyleManager.styles.ErrorTip.fontFamily = "Arial";
StyleManager.styles.ErrorTip.fontSize = "9";
var s1 = StyleManager.styles.ErrorTip.getStyle("fontWeight");
address.text = s1;
}
function changeBold() {
StyleManager.styles.ErrorTip.fontWeight = "bold";
StyleManager.styles.ErrorTip.fontFamily = "Helvetica";
StyleManager.styles.ErrorTip.fontSize = "15";
var s1 = StyleManager.styles.ErrorTip.getStyle("fontWeight");
address.text = s1;
}
]]>
</mx:Script>
<mx:Model id="order">
<name>{name.text}</name>
<address>{address.text}</address>
<city>{city.text}</city>
<zip>{zip.text}</zip>
<email>{email.text}</email>
</mx:Model>
<RequiredFieldValidator field="order.name"/>
<RequiredFieldValidator field="order.city"/>
<mx:EmailValidator field="order.email"/>
<mx:Form>
<mx:FormItem label="Name" required="true">
<mx:Text text="下面输入字符: 1 即有错误提示"/>
<mx:TextInput id="name" width="200"/>
</mx:FormItem>
<mx:FormItem label="Address">
<mx:TextInput id="address" width="200"/>
</mx:FormItem>
<mx:FormItem label="City" required="true">
<mx:TextInput id="city" width="200"/>
</mx:FormItem>
<mx:FormItem label="Zip" required="true">
<mx:TextInput id="zip" width="100"/>
</mx:FormItem>
<mx:FormItem label="Email" required="true">
<mx:TextInput id="email" width="200"/>
</mx:FormItem>
</mx:Form>
<mx:HBox>
<mx:Button label="一般显示" click="changeNormal()"/>
<mx:Button label="加大加粗" click="changeBold()"/>
</mx:HBox>
</mx:Application>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
xmlns="*" verticalGap="0">
<mx:Script>
<![CDATA[
import mx.styles.StyleManager;
function changeNormal() {
StyleManager.styles.ErrorTip.fontWeight = "nomal";
StyleManager.styles.ErrorTip.fontFamily = "Arial";
StyleManager.styles.ErrorTip.fontSize = "9";
var s1 = StyleManager.styles.ErrorTip.getStyle("fontWeight");
address.text = s1;
}
function changeBold() {
StyleManager.styles.ErrorTip.fontWeight = "bold";
StyleManager.styles.ErrorTip.fontFamily = "Helvetica";
StyleManager.styles.ErrorTip.fontSize = "15";
var s1 = StyleManager.styles.ErrorTip.getStyle("fontWeight");
address.text = s1;
}
]]>
</mx:Script>
<mx:Model id="order">
<name>{name.text}</name>
<address>{address.text}</address>
<city>{city.text}</city>
<zip>{zip.text}</zip>
<email>{email.text}</email>
</mx:Model>
<RequiredFieldValidator field="order.name"/>
<RequiredFieldValidator field="order.city"/>
<mx:EmailValidator field="order.email"/>
<mx:Form>
<mx:FormItem label="Name" required="true">
<mx:Text text="下面输入字符: 1 即有错误提示"/>
<mx:TextInput id="name" width="200"/>
</mx:FormItem>
<mx:FormItem label="Address">
<mx:TextInput id="address" width="200"/>
</mx:FormItem>
<mx:FormItem label="City" required="true">
<mx:TextInput id="city" width="200"/>
</mx:FormItem>
<mx:FormItem label="Zip" required="true">
<mx:TextInput id="zip" width="100"/>
</mx:FormItem>
<mx:FormItem label="Email" required="true">
<mx:TextInput id="email" width="200"/>
</mx:FormItem>
</mx:Form>
<mx:HBox>
<mx:Button label="一般显示" click="changeNormal()"/>
<mx:Button label="加大加粗" click="changeBold()"/>
</mx:HBox>
</mx:Application>
RequiredFieldValidator.as文件内容:
class RequiredFieldValidator extends mx.validators.Validator {
function doValidation(value) : Void {
if (value == "1" || value == null ) {
validationError("required", "请输入名字", "123");
}
if (value == "2" ) {
validationError("required", "error");
}
}
}
function doValidation(value) : Void {
if (value == "1" || value == null ) {
validationError("required", "请输入名字", "123");
}
if (value == "2" ) {
validationError("required", "error");
}
}
}