2013-01-02 18 views
0

我有一個Booktitle,Author.I表單,需要驗證這兩個字段,如果它們中的任何一個都是空的,它應顯示消息,因爲字段是強制性的。我做到了這一點。我已經在谷歌搜索了這個,並修改了下面的代碼。如何使用jquery驗證liferay6.1中的表單

<script> 
    $(document).ready(function() { 
     $("#_fm").validate(); 
     var element = document.getElementById("_bookTitle"); 
     element.className = element.className + " required"; 
     element = document.getElementById("_author"); 
     element.className = element.className + " required"; 
    }); 
</script> 
<aui:form name="fm" method="POST" action="<%=updateBookURL.toString()%>"> 
    <aui:input name="bookTitle" label="Book Title" /> 
    <aui:input name="author" /> 
    <aui:button type="submit" value="Save" /> 
</aui:form> 

和portlet.xml的是:

<portlet> 
     <portlet-name>libraryportlet</portlet-name> 
     <icon>/icon.png</icon> 
     <instanceable>false</instanceable> 
     <header-portlet-css>/css/main.css</header-portlet-css> 
     <header-portlet-javascript>https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js</header-portlet-javascript> 
     <header-portlet-javascript>https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.15/jquery-ui.min.js</header-portlet-javascript> 
     <header-portlet-javascript> 
      http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js 
     </header-portlet-javascript> 
     <footer-portlet-javascript> 
      /js/main.js 
     </footer-portlet-javascript> 

     <css-class-wrapper>libraryportlet-portlet</css-class-wrapper> 
    </portlet> 

但沒有success.Where我做wrong.Can任何人告訴你me.Thank。

+0

我建議你不要使用jquery/javascript進行驗證。 Javascript運行客戶端,很容易禁用。 –

回答

0

你可以使用liferay自己的Alloy驗證器來驗證你的表單,這樣就不需要使用jquery和jquery驗證表單驗證。

首先,你需要輸入驗證類到你的JSP:

<%@ page import="com.liferay.portal.kernel.util.Validator" %> 

然後你可以去定義每個輸入字段的規則:

<aui:form name="fm" method="POST" action="<%=updateBookURL.toString()%>"> 
    <aui:input name="bookTitle" label="Book Title"> 
     <aui:validator name="required" /> 
    </aui:input> 

    <aui:input name="author"> 
     <aui:validator name="required" /> 
    </aui:input> 

    <aui:button type="submit" value="Save" /> 
</aui:form> 

這樣可防止如果任何字段爲空,則提交表單。正如Bondye所說,你可能還想做一些服務器端的驗證,因爲Alloy運行在Javascript之上,用戶可以很容易地禁用它,從而在你提交一個空表單的情況下將你的應用程序暴露給一些未處理的異常。