如何将Ajax验证添加到以编程方式生成的Primefaces组件 [英] How to add ajax validation to programmatically generated primefaces component

查看:187
本文介绍了如何将Ajax验证添加到以编程方式生成的Primefaces组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将普通的bean验证附加到以编程方式生成的Primefaces UIComponent上的blur事件.

如果我使用xhtml手动创建组件,效果很好,则它看起来像这样:

<p:inputText id="customerName" value="#{editCustomerBean.name}" label="Name">
    <p:ajax async="true" event="blur" update="customerName, customerNameMsg" />
</p:inputText>

不幸的是,由于一些动态属性将基于运行时数据进行填充,因此我需要即时生成此组件.我编写的尝试完全复制此组件的代码:

UIInput input = new InputText();

AjaxBehavior ajax = new AjaxBehavior();
ajax.setAsync(true);
ajax.setUpdate("customerName, customerNameMsg");
input.addClientBehavior("blur", ajax);
input.setId("customerName");
input.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{editCustomerBean.name}", String.class));

以这种方式生成此组件时,我看到在blur事件上向服务器发送了一个请求,但没有进行任何验证.发布的请求看起来与我在xhtml中指定组件时发送的请求相同:

javax.faces.partial.ajax=true&javax.faces.source=mainForm%3AcustomerName&javax.faces.partial.execute=mainForm%3AcustomerName&javax.faces.partial.render=mainForm%3AcustomerName+mainForm%3AcustomerNameMsg&javax.faces.behavior.event=blur&javax.faces.partial.event=blur&mainForm%3AcustomerName=&javax.faces.ViewState=8176624577669857830%3A-4154840965136338204

我已经在该网站和Primefaces论坛上看到了类似的问题,但通常涉及将侦听器方法附加到AjaxBehavior,这不是我在此尝试做的.我希望在未指定用于验证字段的侦听器的情况下,其行为与标记相同.

解决方案

原来,我在看ajax组件时吠叫错误.今天我意识到我的组件也没有在提交时得到验证.事实证明,当动态创建JSF组件时,需要向该组件手动注册BeanValidator.感谢victor herrera对这个问题的回答: https://stackoverflow.com/a/7055586/1535568

I am trying to attach normal bean validation to the blur event on a programmatically generated Primefaces UIComponent.

If I manually create the component using xhtml, which works fine, it looks like this:

<p:inputText id="customerName" value="#{editCustomerBean.name}" label="Name">
    <p:ajax async="true" event="blur" update="customerName, customerNameMsg" />
</p:inputText>

Unfortunately I need to generate this component on the fly because of some dynamic attributes that will be populated based on runtime data. The code I wrote to try and exactly replicate this component:

UIInput input = new InputText();

AjaxBehavior ajax = new AjaxBehavior();
ajax.setAsync(true);
ajax.setUpdate("customerName, customerNameMsg");
input.addClientBehavior("blur", ajax);
input.setId("customerName");
input.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{editCustomerBean.name}", String.class));

When I generate this component in this way, I see a request sent to the server on the blur event, but no validation takes place. The request which is posted looks identical to that which is sent when I specify the component in xhtml:

javax.faces.partial.ajax=true&javax.faces.source=mainForm%3AcustomerName&javax.faces.partial.execute=mainForm%3AcustomerName&javax.faces.partial.render=mainForm%3AcustomerName+mainForm%3AcustomerNameMsg&javax.faces.behavior.event=blur&javax.faces.partial.event=blur&mainForm%3AcustomerName=&javax.faces.ViewState=8176624577669857830%3A-4154840965136338204

I have seen similar questions posted on this website and the Primefaces forums, but it usually involves attaching a listener method to the AjaxBehavior, which is not what I am trying to do here. I would like the behavior to be the same as the tag when no listener is specified, which is to validate the field.

解决方案

Turns out I was barking up the wrong tree looking at the ajax component. I realized today that my components were not being validated on submit either. It turns out that when you create your JSF components dynamically, you need to manually register the BeanValidator with the component. Thanks to victor herrera for his response to this question: https://stackoverflow.com/a/7055586/1535568

这篇关于如何将Ajax验证添加到以编程方式生成的Primefaces组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆