【问题标题】:cross field validation for composite components复合组件的跨领域验证
【发布时间】:2011-07-20 11:59:39
【问题描述】:

如何实现复合组件的跨字段验证?我使用的复合组件是一个输入文本框(一个用于电子邮件,第二个用于确认电子邮件)。我为 confirmEmail 组件应用了 f:validator 标签。如何在 validate 方法中获取 email 复合组件的值。是 UIComponent 还是 UINamingContainer?

【问题讨论】:

标签: validation jsf jsf-2 composite-component


【解决方案1】:

我已经实现了上面讨论的 validate 方法。我认为我的代码的问题是在复合组件上使用 f:validate 标记的位置。

<eg:inputText id="confirmEmail" value="backingbean.email"/>

<eg:inputText id="email" value="backingbean.email">
<f:validator validatorId="core.jsf.CompareValidator" for="inputText"/>
</eg:inputText>

但是当我提交表单时,验证器没有被调用。我应该将验证器包装在组件周围还是这是正确的实现方式。

【讨论】:

    【解决方案2】:

    从技术上讲,您的组合是一个 UINamingcontainer,但任何组件都可以找到它的子级。

    我怀疑像下面这样的东西应该可以工作

    public void validate(FacesContext context, UIComponent component, Object value) {
        UIInput first = (UIInput)component.findComponent("compositesFirstInputID");
        UIInput second = (UIInput)component.findComponent("compositessecondInputID");
    
        Object firstEntry = first.getSubmittedValue();
        Object secondEntry = second.getSubmittedValue();
        if(!firstEntry.equals(secondEntry))
            throw new ValidatorException(...);
    }
    

    可能想要添加一些空值检查,可能是 trim() 并使用 equlasIgnoreCase。

    【讨论】:

    • @deepti 误读了您的问题 - 以为您有一个带有两个输入的复合材料,而使用页面的复合引用有一个子 f:validate。
    猜你喜欢
    • 2021-11-06
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    相关资源
    最近更新 更多