非欄位校正器vs欄位校正器

來源:互聯網
上載者:User

這裡有兩種方法在你的-validation.xml檔案裡定義校正器
1.<validator>
2.<field-validator>
 當你使用這兩種方法時請將下面的禁忌於心。
非欄位校正器:你可以使用<validator>標籤定義兩種類型的校正器(抽象出來的校正器或具體欄位的校正器)
  view plaincopy to clipboardprint?
01.---------------------------這個沒有綁定到具體的欄位 
02.<validator type="expression> 
03.    <param name="expression">foo gt bar</param> 
04.    <message>foo must be great than bar.</message> 
05.</validator> 
06.---------------------------這個綁定到了bar欄位  
07.<validator type="required"> 
08.    <param name="fieldName">bar</param> 
09.    <message>You must enter a value for bar.</message> 
10.</validator>   ---------------------------這個沒有綁定到具體的欄位
<validator type="expression>
    <param name="expression">foo gt bar</param>
    <message>foo must be great than bar.</message>
</validator>
---------------------------這個綁定到了bar欄位
<validator type="required">
    <param name="fieldName">bar</param>
    <message>You must enter a value for bar.</message>
</validator>   
欄位校正器:<field-validator>和<validator>只有一點不同, 那就是<field-validator>的fieldName屬性繼承自他的父標籤<field>標籤。
<field>標籤通過制定name屬性為他的子標籤綁定該指定欄位,如:<field name="email_address"> 。
採用這種結構的好處,很容易把針對某一個欄位的校正器集合到一個校正器組,否則的話你每個<validator>標籤都要寫一個name屬性,我的天啊。
 在<field>標籤下定義<field-validator>標籤比在每一個<validator>標籤下定義fieldname參數更清晰。(欄位組更清晰)
 注意:你只能在field標籤裡面定義<field-validator>元素,而不能定義抽象的<validator>元素。
如果你在<field>標籤裡面定義<validator>將導致錯誤,因為這在 xwork-validator-1.0.2.dtd是不允許的 在定義
 欄位校正器時請遵循以下文法規則:
view plaincopy to clipboardprint?
01.<field name="email_address"> 
02.    <field-validator type="required"> 
03.        <message>You cannot leave the email address field empty.</message> 
04.    </field-validator> 
05.    <field-validator type="email"> 
06.        <message>The email address you entered is not valid.</message> 
07.    </field-validator> 
08.</field>  <field name="email_address">
    <field-validator type="required">
        <message>You cannot leave the email address field empty.</message>
    </field-validator>
    <field-validator type="email">
        <message>The email address you entered is not valid.</message>
    </field-validator>
</field>
 你可以選擇之一。你使用非欄位校正,並且每一次都為他設定fieldname屬性是完全合法的。下面兩種方式是一樣的。
  view plaincopy to clipboardprint?
01.<field name="email_address"> 
02.    <field-validator type="required"> 
03.        <message>You cannot leave the email address field empty.</message> 
04.    </field-validator> 
05.    <field-validator type="email"> 
06.        <message>The email address you entered is not valid.</message>
07.    </field-validator> 
08.</field> 
09.   
10.<validator type="required"> 
11.    <param name="fieldName">email_address</param> 
12.    <message>You cannot leave the email address field empty.</message> 
13.</validator> 
14.<validator type="email"> 
15.    <param name="fieldName">email_address</param> 
16.    <message>The email address you entered is not valid.</message>  17.</validator>   <field name="email_address">
    <field-validator type="required">
        <message>You cannot leave the email address field empty.</message>
    </field-validator>
    <field-validator type="email">
        <message>The email address you entered is not valid.</message>
    </field-validator>
</field>
 
<validator type="required">
    <param name="fieldName">email_address</param>
    <message>You cannot leave the email address field empty.</message>
</validator>
<validator type="email">
    <param name="fieldName">email_address</param>
    <message>The email address you entered is not valid.</message>
</validator>          
欄位校正器:一個<field>綁定一個欄位,一個<field>可以有多個<field-validator>,對一個欄位可以有多個校正。     
結構清晰,方便瀏覽和檢查。易於維護。    
非欄位校正器:一個<validator>標籤下面只能對一個欄位進行校正,如果對一個欄位進行多次校正,需要寫很多個<validator>,很繁瑣。而且不利於維護。    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.