Omnifaces validateBean:将消息附加到特定组件 [英] Omnifaces validateBean: attach message to specific component

查看:78
本文介绍了Omnifaces validateBean:将消息附加到特定组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Omnifaces <o:validateBean /> 在类级别约束下使用JSR303 bean验证.

I am using Omnifaces <o:validateBean /> to use JSR303 bean validation with a class level constraint.

整个过程效果很好.验证消息显示在Primefaces <p:messages />组件中.现在我想知道是否可以将错误消息的范围缩小到更精确的组件(即特定元素上的<p:message />标签)?在下面的示例中,我想将错误消息引导至带有id="id_msg_anniversary"<p:message />组件(或者如果可能的话,引导至id="id_msg_anniversary"id="id_msg_wedding").

The whole thing works quite good. The validation message is displayed in a Primefaces <p:messages /> component. Now I wanted to know whether it is possible to narrow the error message to a more precise component (i. e. a <p:message /> tag on a specific element)? In the example below I want to direct the error message to the <p:message /> component with id="id_msg_anniversary" (or if that is possible, to both id="id_msg_anniversary" and id="id_msg_wedding").

我的目的之一是通过bean验证来验证所有内容,而不要使用<o:validateOrder /><o:validateMultiple />.

One of my intention is to validate everything with bean validation and not to use <o:validateOrder /> or <o:validateMultiple />.

@DayIsBeforeAnniversary
@Entity
public class Person implements Serializable
{
    @Temporal(TemporalType.DATE)
    private Date weddingDay;

    @Temporal(TemporalType.DATE)
    private Date silverWeddingAnniversary;
    ...
}

<p:messages id="id_messages" />

<h:outputLabel value="Wedding Day" />
<p:message id="id_msg_wedding" display="icon" for="id_wedding" />
<p:calendar 
    id="id_wedding" 
    value="#{personController.person.weddingDay}"> <br />

<h:outputLabel value="Silver Wedding Anniversary" />
<p:message id="id_msg_anniversary" display="icon" for="id_anniversary" />
<p:calendar 
    id="id_anniversary" 
    value="#{personController.person.silverWeddingAnniversary}" /> <br/>

<p:commandButton 
    value="test" 
    action="#{personController.navigate()}" 
    update="@all"/>

<o:validateBean value="#{personController.person}" />

public class DayIsBeforeAnniversaryValidator implements ConstraintValidator<DayIsBeforeAnniversary, Person>
{
    @Override
    public boolean isValid(Person person, ConstraintValidatorContext context)
    {
        return person == null
            || person.getWeddingDay() == null
            || person.getSilverWeddingAnniversary() == null
            || person.getWeddingDay().before(person.getSilverWeddingAnniversary());
    }
    ...
}

public @interface DayIsBeforeAnniversary { ... }

为澄清起见,我可以附加更多代码.

For clarification I could attach more code.

推荐答案

<o:validateBean>将其消息与<h:form>关联.因此,您需要做的就是确保在所需位置放置<p:message for="formId">.请注意,您可以有多个.以下是启动示例.

The <o:validateBean> associates its message with the <h:form>. So, all you need to do is to make sure you have a <p:message for="formId"> at the desired place. Do note that you can have multiple of them. Below is a kickoff example.

<h:form id="formId">
    <p:inputText id="foo" ... />
    <p:message for="foo" />
    <p:message for="formId" />
    ...
    <p:inputText id="bar" ... />
    <p:message for="bar" />
    <p:message for="formId" />
    ...
    <o:validateBean ... />
</h:form>

但是,我确实同意这不是最优雅的解决方案,与

I however do agree this is not the most elegant solution, surely not when compared with members of ValidateMultipleFields family which has an useful showMessageFor attribute for the purpose. It's only unfortunately technically pretty convoluted to implement it on <o:validateBean> too as there's technically no direct relationship between BV constraint violation errors and JSF input fields, so it has to be manually inspected based on bean properties and JSF component tree, which is not exactly an oneliner work.

这篇关于Omnifaces validateBean:将消息附加到特定组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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