JQuery 验证引擎和 primefaces commandButton

发布于 2024-11-08 14:27:39 字数 289 浏览 0 评论 0 原文

我目前在使用客户端验证时遇到一些问题。我正在使用 Jquery 验证引擎插件,但无法使其与 PrimeFaces commandButton 一起使用。 我认为问题在于 primefaces commandButton 不是像您使用的那样的普通提交按钮,现在我的问题是如何让它工作,以便当我单击客户端验证时触发,导致仅使用服务器端为了用户体验,我想避免验证。

我想在其上使用它的页面是通过 a 动态包含的,但据我所知,这不会导致问题。

我正在使用带有 Facelets 和 PrimeFaces 3.0M1 的 JSF 2.0 :D

I am currently having some trouble using my client side validation. I am using the Jquery Validation Engine plugin and I can't get it to work with PrimeFaces commandButton.
I think the problem is that the primefaces commandButton isn't a normal submit button like you have using and now my question is how do I get it to work so that when I click on my client side validation is triggered, cause using only server side validation is something I want to avoid for the user experience.

The page I want to use it on is dynamically include through a but that doesn't cause the problem according to me.

I am using JSF 2.0 with Facelets and PrimeFaces 3.0M1 :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青春如此纠结 2024-11-15 14:27:40

让 JSF 通过 Ajax 进行验证即可。添加 ,用于处理当前输入字段并在 模糊 事件触发(例如,当您退出该字段时)。

<h:inputText id="foo" value="#{bean.foo}" required="true">
    <f:ajax event="blur" render="fooMessage" />
</h:inputText>
<h:message id="fooMessage" for="foo" />

无需通过 jQuery 重复/接管验证。

另请参阅:

Just let JSF validate by Ajax. Add a <f:ajax> or <p:ajax> which processes the current input field and re-renders the associated message when the blur event fires (when you tab out the field, e.g.).

<h:inputText id="foo" value="#{bean.foo}" required="true">
    <f:ajax event="blur" render="fooMessage" />
</h:inputText>
<h:message id="fooMessage" for="foo" />

No need to duplicate/takeover the validation by jQuery.

See also:

风为裳 2024-11-15 14:27:40

事实证明非常简单
只需添加对 p:commandButton onclick 事件的处理

<p:commandButton value="#{msg['forecast']}" onclick="validator.validate();" update="somegrid" actionListener="#{someBean.someAction}" styleClass="ui-priority-primary" />

然后只需常规 JQuery 验证器代码

var validator = null;
$(document).ready(function () { 
    validator = $("form[id='forecastinput']").validate({ 
    //rules, messages, etc. 
    }); 
});

It turns out to be pretty simple
Just add handling for your p:commandButton onclick event

<p:commandButton value="#{msg['forecast']}" onclick="validator.validate();" update="somegrid" actionListener="#{someBean.someAction}" styleClass="ui-priority-primary" />

And then just regular JQuery validator code

var validator = null;
$(document).ready(function () { 
    validator = $("form[id='forecastinput']").validate({ 
    //rules, messages, etc. 
    }); 
});
自在安然 2024-11-15 14:27:40

你试过这个吗?

$("#buttonId").click(functionName);

Have you tried this ?

$("#buttonId").click(functionName);

听风念你 2024-11-15 14:27:40

如果您查看 Primefaces 命令按钮的标记,您会发现它默认使用类型为“提交”的按钮标签。

这只是您将看到的示例。注意到,我不确定如何在这样的按钮上使用 Jquery 验证。就用户体验而言,服务器端验证都是通过 AJAX 完成的,因此对用户来说是透明的。我认为您避免这种情况的推理是不公正的,除非您的推理确实是性能是一个巨大的问题或者在视觉上它不满足您的业务需求。

If you look at the markup of the Primefaces command button you will see it uses the button tag with a type of "submit" by default.

<button type="submit" style="height: 22px; line-height: 22px; font-size: 10px ! important;" onclick="PrimeFaces.ajax.AjaxRequest('/webapp/admin/listBadges.xhtml',{formId:'AddBadgesForm',async:false,global:true,source:'AddBadgesForm:j_idt54',process:'@all',update:'listBadgesForm:badgeList AddBadgesForm'});return false;" name="AddBadgesForm:j_idt54" id="AddBadgesForm:j_idt54" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" role="button" aria-disabled="false"><span class="ui-button-icon-primary ui-icon icoIDAdd"></span><span class="ui-button-text">  Add Badge</span></button>

This is just an example of what you would see. Noting that, I am not sure how one would use Jquery validation on a button like this. As far as the user experience is considered however the server side validation is all done through AJAX so it is transparent to the user. I feel your reasoning for avoiding this is unjust unless your reasoning really is that performance is a huge concern or that visually it doesn't meet your business requirements.

薄暮涼年 2024-11-15 14:27:40

JSF 无法与 JQuery 验证插件一起开箱即用。

您必须对其进行调整才能使其正常工作。这是因为 JSF 使用 AJAX 发布表单,而不是默认的 form.post 方法。

使用 h:commandlink 和 h:commandbutton 发布时使用它来链接 jquery 验证:

//executes code before running default onclick behavior
//@param domId dom element id for the object which onclick method is being intercepted
//@param code a function holding the code to run before executing the original onclick method. This function should return true if original method is to be performed
function triggerBeforeOnClick(domId, code){
    if(document.getElementById){
        var commandLink = document.getElementById(domId);
        var commandLinkOnClick = commandLink.onclick;
        commandLink.onclick = function(){
            if(code()){
                return commandLinkOnClick();
            }
            return false;
        }
    }
}

将 domId 替换为

'formId:commandLinkIDOrCommandButtonID'

确保在 $(document).ready(function( ... )); 中以及加载 jquery 和验证库之后使用它。

您还必须手动为每个输入字段指定验证规则(忽略有关此问题的 jquery 文档),如下所示:

$(document.getElementById('formId:inputId')).rules('add', {
      required: true
      , messages: {
         required: 'error message'
      }
});

JSF won't work out of the box with JQuery validate plugin.

You must tweak it to make it work. This is because JSF posts forms using AJAX and not the default form.post method.

Use this to chain jquery validation when posting with h:commandlink and h:commandbutton:

//executes code before running default onclick behavior
//@param domId dom element id for the object which onclick method is being intercepted
//@param code a function holding the code to run before executing the original onclick method. This function should return true if original method is to be performed
function triggerBeforeOnClick(domId, code){
    if(document.getElementById){
        var commandLink = document.getElementById(domId);
        var commandLinkOnClick = commandLink.onclick;
        commandLink.onclick = function(){
            if(code()){
                return commandLinkOnClick();
            }
            return false;
        }
    }
}

replace domId with

'formId:commandLinkIDOrCommandButtonID'

Make sure you use it within $(document).ready(function( ... )); and after having loaded jquery and validate libraries.

You must also specify validation rules for each input field by hand (disregard jquery docs on this matter) like this:

$(document.getElementById('formId:inputId')).rules('add', {
      required: true
      , messages: {
         required: 'error message'
      }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文