简体   繁体   English

显示多个具有h:message的字段的JSF验证错误

[英]Showing JSF validation errors for several fields with more than h:message

A form with validations on several input fields should show stylish error messages with an icon in front of the message contained in a red box. 在多个输入字段上进行了验证的表单应显示时尚的错误消息,并在红色框中包含的消息前面带有一个图标。

The h:panelGroup containing icon and message must be rendered only in case of an error. 仅在出现错误的情况下,才必须渲染包含图标和消息的h:panelGroup。

With just one validated input field this would work: 仅使用一个经过验证的输入字段,它将起作用:

<h:panelGroup rendered="#{facesContext.validationFailed}"

But with more than one input field all error panel groups are visible, even those without error showing no text but the icon in a red box. 但是通过多个输入字段,所有错误面板组都是可见的,即使那些没有错误的组也没有显示任何文字,而是在红色框中显示了图标。

My solution is now to have a validator for each input and add an attribute ´´validationFailed´´ to each validator. 我现在的解决方案是为每个输入都有一个验证器,并向每个验证器添加一个属性“ validationFailed”。

<p:inputNumber id="mileage" validator="#{mileageValidator.validate}"></p:inputNumber>
<h:panelGroup rendered="#{mileageValidator.validationFailed}" styleClass="wizard-alert-box">
    <div class="wizard-alert-content-margin">
        <h:graphicImage name="attention.png"/>
        <h:message id="invalid-mileage" for="mileage" showSummary="true" showDetail="false"/>
    </div>
</h:panelGroup>

<p:calendar id="date" validator="#{dateValidator.validate}"></p:calendar>
<h:panelGroup rendered="#{dateValidator.validationFailed}" styleClass="wizard-alert-box">
    <div class="wizard-alert-content-margin">
        <h:graphicImage name="attention.png"/>
        <h:message id="invalid-date" for="date" showSummary="true" showDetail="false"/>
    </div>
</h:panelGroup>

And here is one of the validators: 这是验证器之一:

@Named("mileageValidator")
public class MileageValidator {

    @Getter
    private boolean validationFailed;

    public void validate(FacesContext context, UIComponent component, Object value) {
        validationFailed = value == null;
        if (validationFailed) {
            throw new ValidatorException(new FacesMessage("mileage empty"));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM