2012-05-17 38 views
0

對於Struts 1驗證XML文件,是否可以說字段x OR字段y是必需的?Struts 1驗證字段x OR y必需

下面是我在XML文件中的語法:

<form name="/save"> 
    <field property="email" depends="required"> 
      <arg0 key="Email" resource="false"/> 
     </field> 
    <field property="phone" depends="required"> 
     <arg0 key="Phone" resource="false"/> 
    </field> 
</form> 

上面的代碼需要電子郵件和電話進行填寫。我想要求電子郵件或電話。

這可能在Struts 1驗證器中嗎?我知道我可以在java代碼中做到這一點,但我很好奇它是否可以在struts 1的validation.xml文件中完成。

由於

回答

0

作爲一種變通方法,我基於從this post on stackoverflow指令與我的定製邏輯添加一個validate方法到我的ActionForm。

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 
    ActionErrors errors = new ActionErrors(); 
    if ((phone==null) && (email == null)) 
     errors.add("email", new ActionError("error.phoneOrEmail")); 
    return errors; 
}