Omnifaces validateBean:将消息附加到特定组件
Omnifaces validateBean: attach message to specific component
我正在使用 Omnifaces <o:validateBean />
来使用具有 class 级别约束的 JSR303 bean 验证。
整体效果很好。验证消息显示在 Primefaces <p:messages />
组件中。现在我想知道是否可以将错误消息缩小到更精确的组件(即特定元素上的 <p:message />
标记)?在下面的示例中,我想将错误消息定向到具有 id="id_msg_anniversary"
的 <p:message />
组件(或者如果可能的话,定向到 id="id_msg_anniversary"
和 id="id_msg_wedding"
)。
我的目的之一是使用 bean 验证来验证所有内容,而不是使用 <o:validateOrder />
或 <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 { ... }
为了说明,我可以附上更多代码。
<o:validateBean>
将其消息与 <h:form>
相关联。因此,您需要做的就是确保在所需位置有一个 <p:message for="formId">
。请注意,您可以拥有多个。下面是一个启动示例。
<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>
不过,我同意这不是最优雅的解决方案,与 ValidateMultipleFields
家族的成员相比肯定不是,因为 showMessageFor
家族的成员有一个有用的 showMessageFor
属性。不幸的是,在 <o:validateBean>
上实现它在技术上也非常复杂,因为 BV 约束违反错误和 JSF 输入字段之间在技术上没有直接关系,因此必须根据 bean 属性和 JSF 组件树手动检查它,这是不完全是单行本作品。
我正在使用 Omnifaces <o:validateBean />
来使用具有 class 级别约束的 JSR303 bean 验证。
整体效果很好。验证消息显示在 Primefaces <p:messages />
组件中。现在我想知道是否可以将错误消息缩小到更精确的组件(即特定元素上的 <p:message />
标记)?在下面的示例中,我想将错误消息定向到具有 id="id_msg_anniversary"
的 <p:message />
组件(或者如果可能的话,定向到 id="id_msg_anniversary"
和 id="id_msg_wedding"
)。
我的目的之一是使用 bean 验证来验证所有内容,而不是使用 <o:validateOrder />
或 <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 { ... }
为了说明,我可以附上更多代码。
<o:validateBean>
将其消息与 <h:form>
相关联。因此,您需要做的就是确保在所需位置有一个 <p:message for="formId">
。请注意,您可以拥有多个。下面是一个启动示例。
<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>
不过,我同意这不是最优雅的解决方案,与 ValidateMultipleFields
家族的成员相比肯定不是,因为 showMessageFor
家族的成员有一个有用的 showMessageFor
属性。不幸的是,在 <o:validateBean>
上实现它在技术上也非常复杂,因为 BV 约束违反错误和 JSF 输入字段之间在技术上没有直接关系,因此必须根据 bean 属性和 JSF 组件树手动检查它,这是不完全是单行本作品。