【问题标题】:How to validate a form in liferay6.1 using jquery如何使用 jquery 在 liferay6.1 中验证表单
【发布时间】:2012-12-16 16:35:14
【问题描述】:

我有一个带有 Booktitle,Author 的表单。我需要验证这两个字段,如果其中任何一个为空,它应该显示消息,因为字段是强制性的。我怎样才能实现这一点。我已经在 google 中搜索过这个,并修改如下代码。

<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>

但没有成功。我在哪里做错了。谁能告诉我。谢谢。

【问题讨论】:

  • 我建议您不要使用 jquery/javascript 进行验证。 Javascript 在客户端运行,很容易禁用。

标签: jquery validation liferay-6


【解决方案1】:

您可以使用 liferay 自己的 Alloy 验证器来验证您的表单,这样就无需使用 jquery 和 jquery validate 进行表单验证。

首先,您需要将验证器类导入您的 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 之上,用户可以轻松禁用它,从而在提交空表单的情况下将您的应用程序暴露给一些未处理的异常。

【讨论】:

    猜你喜欢
    • 2017-05-17
    • 2019-04-14
    • 1970-01-01
    • 2023-03-19
    • 2012-07-11
    • 2020-12-21
    • 2019-04-27
    • 1970-01-01
    • 2017-07-17
    相关资源
    最近更新 更多