通过AJAX禁用验证 [英] Disable validator via ajax

查看:132
本文介绍了通过AJAX禁用验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的请求范围的实体/ POJO其中有一个枚举和一个字符串作为属性。

I have a simple request scoped entity / pojo which has a Enum and a String as properties.

public Enum Type
{
    None,
    Email,
    Fax;
}

@ManagedBean(name = "testEntity")
@RequestScoped
public class TestEntity
{
    private Type type; //Default = None
    private String address;

    //getter and setter
}

这枚举有一个字段电子邮件该标识的电子邮件地址与相关地址。
在JSF我现在想启用/禁用有关在selectOneMenu用于当前所选类型的地址inputText域的验证。

This Enum has a field 'Email' which identifies a e-mail address with a related address.
In JSF I now want to enable/disable a validator of a address InputText field regarding the currently selected type in a SelectOneMenu.

<h:form id="formId">
    <p:selectOneMenu id="type" value="#{testEntity.type}>
        <p:ajax event="change" update=":formId:address"/>
        <f:selectItem itemLabel="E-mail" itemValue="Email"/>
        <f:selectItem itemLabel="Fax" itemValue="Fax"/>
    </p:selectOneMenu>
    <p:inputText id="address" value="#{testEntity.address}">
        <f:validator validatorId="emailValidator" disabled="#{testEntity.type != 'Email'}"/>
    </p:inputText>
    <!-- button to call bean method with testEntity as param -->
</h:form>

这不是工作在验证是永远不会主动但是Ajax调用工作,因为我可以看到在其他领域的变化值。

It is not working the validator is never active but the ajax call is working since I can see the change value in other fields.

推荐答案

这是不幸的是不可能的。该&LT; F:XXX&GT; 标签taghandlers(不是UI组件),它在视图生成时运行,而不是在视图渲染时间。所以,如果它认为,建设过程中禁用,它会一直被禁止,直到视图重建(由新的请求还是非空导航EG)。

That's unfortunately not possible. The <f:xxx> tags are taghandlers (not UI components) which run during view build time, not during view render time. So if it's disabled during building of the view, it'll always be disabled until the view is recreated (e.g. by new request or a non-null navigation).

您就需要它代表还具有全球性验证的基础上所需要的验证程序的键入属性。

You'd need to have a "global" validator which delegates further to the desired validator based on the type attribute.

例如。

<p:inputText ... validator="#{testEntity.validateAddress}" />

public void validateAddress(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    if (type == Email) {
        context.getApplication().createValidator("emailValidator").validate(context, component, value);
    }
}


更新 OmniFaces 最近增加了一个新的 &LT;○:验证&GT; 这应该解决的正是这种标记问题如下:


Update OmniFaces has recently added a new <o:validator> tag which should solve exactly this problem as follows:

<o:validator validatorId="emailValidator" disabled="#{testEntity.type != 'Email'}"/>

请参阅展柜例如这里

这篇关于通过AJAX禁用验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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