Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf Omnifaces validateBean:将消息附加到特定组件_Jsf_Bean Validation_Omnifaces - Fatal编程技术网

Jsf Omnifaces validateBean:将消息附加到特定组件

Jsf Omnifaces validateBean:将消息附加到特定组件,jsf,bean-validation,omnifaces,Jsf,Bean Validation,Omnifaces,我使用JSR303 bean验证和类级约束 整件事做得很好。验证消息显示在Primefaces组件中。现在我想知道是否有可能将错误消息缩小到更精确的组件(即特定元素上的标记)?在下面的示例中,我想将错误消息发送到id=“id\u msg\u周年纪念日”(或者如果可能,发送到id=“id\u msg\u周年纪念日”和id=“id\u msg\u婚礼”) 我的意图之一是使用bean验证来验证所有内容,而不是使用或 为了澄清,我可以附加更多的代码。该将其消息与关联。因此,您需要做的就是确保在所需的位置

我使用JSR303 bean验证和类级约束

整件事做得很好。验证消息显示在Primefaces
组件中。现在我想知道是否有可能将错误消息缩小到更精确的组件(即特定元素上的
标记)?在下面的示例中,我想将错误消息发送到
id=“id\u msg\u周年纪念日”
(或者如果可能,发送到
id=“id\u msg\u周年纪念日”
id=“id\u msg\u婚礼”

我的意图之一是使用bean验证来验证所有内容,而不是使用

为了澄清,我可以附加更多的代码。

将其消息与
关联。因此,您需要做的就是确保在所需的位置有一个
。请注意,您可以有多个。下面是一个启动示例

<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>

...
...

但是,我确实同意这不是最优雅的解决方案,与具有有用的
showMessageFor
属性的家庭成员相比肯定不是。不幸的是,在
上实现它在技术上也非常复杂,因为BV约束冲突错误和JSF输入字段在技术上没有直接关系,所以必须根据bean属性和JSF组件树手动检查它,这并不是一个简单的工作。

请看一下,谢谢您的快速回复。我基本上知道我可以为特定组件创建消息。但就我而言,这条信息不是由我自己生成的,而是由Omnifaces库生成的。非常感谢!这对我现在来说是可行的。恐怕我还有另一个实体,我想在其中创建多个类级别约束。在这种情况下,我想这种方法会有问题。。。不管怎么说,您是否有机会知道在即将发布的JSF2.3中是否会有一个复杂的解决方案?大约两年后,我仍然在努力进行bean验证。但是有了OmniFaces2.6,我的整个问题有了最终的解决方案。ValidateMultipleFields系列的其他成员中有一个类似的
showMessageFor
属性。谢谢你添加这个!无论如何,我仍然有一些问题,使用它,但这是一个不同的故事。。。()
<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 { ... }
<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>