我想對mxml文件中的三個字段進行驗證。其中兩個是必需的,一個不是。我只想驗證郵政編碼字段,如果輸入的東西。如果它是空白的,那麼我希望允許「提交」按鈕被啓用,只要其他兩個必填字段被正確設置。代碼:靈活條件驗證
<mx:EmailValidator id="emailValidator"
property="text" source="{ownerName}"
valid="emailValidator_validHandler(event)"
invalid="emailValidator_validHandler(event)"/>
<mx:ZipCodeValidator id="zipCodeValidator"
property="text" source="{zip}"
required="false"
valid="zipCodeValidator_validHandler(event)"
invalid="zipCodeValidator_validHandler(event)"/>
<mx:Validator id="roomNameValidator"
property="text" source="{roomName}"
valid="roomNameValidator_validHandler(event)"
invalid="roomNameValidator_validHandler(event)"/>
[Bindable]
private var isRoomNameValid:Boolean;
[Bindable]
private var isOwnerValid:Boolean;
[Bindable]
private var isZipValid:Boolean;
protected function emailValidator_validHandler(event:ValidationResultEvent):void
{
isOwnerValid = (event.type==ValidationResultEvent.VALID);
}
protected function zipCodeValidator_validHandler(event:ValidationResultEvent):void
{
isZipValid = (event.type == ValidationResultEvent.VALID);
}
protected function roomNameValidator_validHandler(event:ValidationResultEvent):void
{
isRoomNameValid = (event.type==ValidationResultEvent.VALID);
}
然後我試圖設置提交按鈕的enabled屬性:
enabled = "{isOwnerValid && isRoomNameValid && zip.text.length>0?isZipValid:true}"
但它不工作。即使郵政編碼字段顯示紅色邊框,我仍然可以在zip字段中提交錯誤的數據。
嗨特里什。感謝你的回答。我試過這段代碼,即使在郵政編碼框周圍出現紅色輪廓,它仍然可以讓我提交錯誤的數據。 – David