云自無心水自閑

          天平山上白云泉,云自無心水自閑。何必奔沖山下去,更添波浪向人間!
          posts - 288, comments - 524, trackbacks - 0, articles - 6
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Flex2.0中使用Validator

          Posted on 2006-04-14 11:14 云自無心水自閑 閱讀(1832) 評論(1)  編輯  收藏 所屬分類: FlexFlex2
          在Flex2.0中, Validator組件的使用方式和1.5中相比, 進行了一些改變, 不再需要定義Model, 可以在Validator屬性中直接引用Form成員了.
          ????<mx:Form?id="loginForm">
          ????????
          <mx:Text?text="?{AtsModelLocator.getInstance().loginFailMessage?}"?width="80%"?color="red"/>
          ????
          ????????
          <mx:FormItem?label="Username:?"?required="true">
          ????????????
          <mx:TextInput?id="username"?/>
          ????????
          </mx:FormItem>

          ????????
          <mx:FormItem?label="Password:?"?required="true">
          ????????????
          <mx:TextInput?id="password"?/>
          ????????
          </mx:FormItem>
          ????
          </mx:Form>


          ????
          <mx:ControlBar>
          ????????
          <mx:Button?id="loginSubmit"?label="Login"?mouseUp="loginUser()"/>
          ????
          </mx:ControlBar>
          ????
          ????
          <mx:StringValidator?id="userNameValidator"?source="{username}"?property="text"
          ????????tooShortError
          ="This?string?is?shorter?than?the?minimum?allowed?length?of?3.?"?
          ????????tooLongError
          ="This?string?is?longer?than?the?maximum?allowed?length?of?20."?
          ????????minLength
          ="4"?maxLength="20"/>

          ????
          <mx:StringValidator?id="userPassValidator"?source="{password}"?property="text"
          ????????tooShortError
          ="This?string?is?shorter?than?the?minimum?allowed?length?of?6.?"?
          ????????tooLongError
          ="This?string?is?longer?than?the?maximum?allowed?length?of?10."?
          ????????minLength
          ="4"?maxLength="20"/>

          這樣就定義好了兩個Validator, 可以對用戶名和用戶密碼進行校驗.
          但是怎么使用這兩個Validator呢?

          我是這樣用的:
          ????<mx:Script>
          ????
          <![CDATA[
          ????????import?mx.controls.Alert;
          ????????import?mx.events.ValidationResultEvent;
          ????????import?mx.validators.ValidationResult;??
          ????????????
          ???????import?com.ats.vo.LoginVO;
          ???????import?com.ats.control.LoginEvent;
          ???????
          ???????import?mx.validators;
          ???????
          ???????public?
          function?loginUser()?:?void
          ???????{
          ??????????
          if?(?!?modelCheckValid?)?{
          ??????????????modelCheckValid?
          =?true;
          ??????????????
          return;
          ??????????}
          ???????????
          ??????????
          var?loginVO?:?LoginVO?=?new?LoginVO();
          ??????????loginVO.username?
          =?username.text;
          ??????????loginVO.password?
          =?password.text;
          ????????????
          ????????????
          var?event?:?LoginEvent?=?new?LoginEvent(?loginVO?);
          ????????????dispatchEvent(?event?);
          ???????}
          ???????
          ???????private?
          var?modelCheckValid?:?Boolean?=?true;
          ????]]
          >
          ????
          </mx:Script>

          ????
          <mx:Form?id="loginForm">
          ????????
          <mx:Text?text="?{AtsModelLocator.getInstance().loginFailMessage?}"?width="80%"?color="red"/>
          ????
          ????????
          <mx:FormItem?label="Username:?"?required="true">
          ????????????
          <mx:TextInput?id="username"?/>
          ????????
          </mx:FormItem>

          ????????
          <mx:FormItem?label="Password:?"?required="true">
          ????????????
          <mx:TextInput?id="password"?/>
          ????????
          </mx:FormItem>
          ????
          </mx:Form>


          ????
          <mx:ControlBar>
          ????????
          <mx:Button?id="loginSubmit"?label="Login"?mouseUp="loginUser()"/>
          ????
          </mx:ControlBar>
          ????
          ????
          <mx:StringValidator?id="userNameValidator"?source="{username}"?property="text"
          ????????tooShortError
          ="This?string?is?shorter?than?the?minimum?allowed?length?of?3.?"?
          ????????tooLongError
          ="This?string?is?longer?than?the?maximum?allowed?length?of?20."?
          ????????minLength
          ="4"?maxLength="20"
          ????????invalid
          ="modelCheckValid=false"
          ????????trigger
          ="{loginSubmit}"
          ????????triggerEvent
          ="mouseDown"/>

          ????
          <mx:StringValidator?id="userPassValidator"?source="{password}"?property="text"
          ????????tooShortError
          ="This?string?is?shorter?than?the?minimum?allowed?length?of?6.?"?
          ????????tooLongError
          ="This?string?is?longer?than?the?maximum?allowed?length?of?10."?
          ????????minLength
          ="4"?maxLength="20"
          ????????invalid
          ="modelCheckValid=false"
          ????????trigger
          ="{loginSubmit}"
          ????????triggerEvent
          ="mouseDown"/>



          為什么這么復雜地在Validator中定義trigger, triggerEvent呢?
          原因是這樣的: 如果不是在Validator的invalid事件中去設置modelCheckValid這個標志量.
          就需要在loginUser()函數中對所有Validator進行判斷, 代碼會顯得比較臃腫復雜.
          而且如果需要考慮是否需要一次性顯示出所有校驗失敗的錯誤.
          代碼示例:
          ????<mx:Script>
          ????
          <![CDATA[
          ????????import?mx.controls.Alert;
          ????????import?mx.events.ValidationResultEvent;
          ????????import?mx.validators.ValidationResult;??
          ????????????
          ???????import?com.ats.vo.LoginVO;
          ???????import?com.ats.control.LoginEvent;
          ???????
          ???????import?mx.validators;
          ???????
          ???????public?
          function?loginUser()?:?void
          ???????{
          ???????
          ??????????
          var?vrEvent?:?ValidateResultEvent;
          ??????????
          ??????????
          var?checkFailed?:?Boolean?=?false;
          ??????????
          ??????????vrEvent?
          =?userNameValidator.validate();
          ??????????
          if?(?vrEvent.results?!=?null?&&?vrEvent.results.length?>?0?)?{
          ??????????????
          //?驗證失敗
          ??????????????checkFailed?=?true;
          ??????????}
          ??????????
          ??????????vrEvent?
          =?userPassValidator.validate();
          ??????????
          if?(?vrEvent.results?!=?null?&&?vrEvent.results.length?>?0?)?{
          ??????????????
          //?驗證失敗
          ??????????????checkFailed?=?true;
          ??????????}
          ??????????
          ??????????
          if?(?checkFailed?)?return;
          ???????????
          ??????????
          var?loginVO?:?LoginVO?=?new?LoginVO();
          ??????????loginVO.username?
          =?username.text;
          ??????????loginVO.password?
          =?password.text;
          ????????????
          ????????????
          var?event?:?LoginEvent?=?new?LoginEvent(?loginVO?);
          ????????????dispatchEvent(?event?);
          ???????}
          ???????
          ????]]
          >
          ????
          </mx:Script>

          ????
          <mx:Form?id="loginForm">
          ????????
          <mx:Text?text="?{AtsModelLocator.getInstance().loginFailMessage?}"?width="80%"?color="red"/>
          ????
          ????????
          <mx:FormItem?label="Username:?"?required="true">
          ????????????
          <mx:TextInput?id="username"?/>
          ????????
          </mx:FormItem>

          ????????
          <mx:FormItem?label="Password:?"?required="true">
          ????????????
          <mx:TextInput?id="password"?/>
          ????????
          </mx:FormItem>
          ????
          </mx:Form>


          ????
          <mx:ControlBar>
          ????????
          <mx:Button?id="loginSubmit"?label="Login"?mouseUp="loginUser()"/>
          ????
          </mx:ControlBar>
          ????
          ????
          <mx:StringValidator?id="userNameValidator"?source="{username}"?property="text"
          ????????tooShortError
          ="This?string?is?shorter?than?the?minimum?allowed?length?of?3.?"?
          ????????tooLongError
          ="This?string?is?longer?than?the?maximum?allowed?length?of?20."?
          ????????minLength
          ="4"?maxLength="20"/>

          ????
          <mx:StringValidator?id="userPassValidator"?source="{password}"?property="text"
          ????????tooShortError
          ="This?string?is?shorter?than?the?minimum?allowed?length?of?6.?"?
          ????????tooLongError
          ="This?string?is?longer?than?the?maximum?allowed?length?of?10."?
          ????????minLength
          ="4"?maxLength="20"/>

          這種方法也是可行的.
          至于具體使用哪一個, 憑自己的喜好了.



          評論

          # re: Flex2.0中使用Validator  回復  更多評論   

          2007-09-27 09:45 by tiangej
          《目的》:
          畫面項目"用戶id"必須是英文數字,且長度在4--20之間。
          于是我計劃用 mx:StringValidator(字符串驗證) 和 mx:RegExpValidator(正則驗證)同時作用于上述的項目上。

          《結果》:
          只有最后定義的驗證項發揮作用。

          《問題》:
          是否能夠對同一輸入項目進行兩種以上的數據校驗(用Validator對象)?
          主站蜘蛛池模板: 自贡市| 察哈| 安吉县| 大城县| 界首市| 奈曼旗| 湟源县| 常熟市| 饶平县| 富平县| 尼勒克县| 温州市| 蒙阴县| 盐津县| 屏东县| 调兵山市| 龙里县| 原平市| 大洼县| 福泉市| 尉氏县| 沭阳县| 五寨县| 乌审旗| 牟定县| 通海县| 台山市| 克什克腾旗| 禄丰县| 龙游县| 玉田县| 宝坻区| 措美县| 峡江县| 和龙市| 河津市| 南郑县| 徐汇区| 合山市| 独山县| 循化|