使用自定义验证器验证失败时如何跳过操作?

时间:2011-05-26 14:30:12

标签: validation jsf

我有一个用于库的小型Web应用程序,带有自定义的ISBN验证程序。我添加图书的.xhtml页面如下所示:

<fieldset>
<h:messages/>
<ul>
    <li>
        <h:outputLabel for="isbn" value="#{labels.isbn}:" />
        <h:inputText id="isbn" value="#{addController.book.isbn.isbnValue}" required="true" requiredMessage="- ISBN must be filled in.">
            <f:validator validatorId="isbnValidator" />
        </h:inputText>
    </li>
    <li>
        <h:outputLabel for="title" value="#{labels.title}:" />
        <h:inputText id="title" value="#{addController.book.title}" required="true" requiredMessage="- Title must be filled in."/>
    </li>
    <li>
        <h:outputLabel for="name" value="#{labels.name}:" />
        <h:inputText id="name" value="#{addController.book.person.name}" required="true" requiredMessage="- Name must be filled in."/>
    </li>
    <li>
        <h:outputLabel for="firstname" value="#{labels.firstname}:" />
        <h:inputText id="firstname" value="#{addController.book.person.firstname}" />
    </li>

</ul>
<h:commandButton id="addButton" action="#{addController.save}" value="#{labels.add}" />
<h:commandButton id="cancelButton" action="bookOverview" value="#{labels.cancel}" />
</fieldset>
</ui:define>

第一个输入字段的isbnValidator是此类:

private Isbn isbn;

@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    isbn = new Isbn((String) value);
    if (!isbn.isIsbn17Characters()) {
        addMessageToContext(context, "- ISBN needs to have 17 characters");
    }
    if (isbn.isIsbn17Characters() && !isbn.isIsbnInRightFormat()) {
        addMessageToContext(context, "- Wrong format, it should be like 'XXX-XX-XXX-XXXX-X'");
    }
    if (isbn.isIsbn17Characters() && isbn.isIsbnInRightFormat() && !isbn.isIsbnFormatValid()) {
        addMessageToContext(context, "- ISBN can only contain numbers, and no other tokens");
    }
    if (isbn.isIsbn17Characters() && isbn.isIsbnInRightFormat() && isbn.isIsbnFormatValid()
            && !isbn.isLastNumberValid()) {
        addMessageToContext(context, "- Last number of the ISBN should be " + isbn.getCorrectLastNumber()
                + " with those 12 numbers");
    }

}

public static void addMessageToContext(FacesContext context, String message) {
    FacesMessage facesMessage = new FacesMessage();
    facesMessage.setSummary(message);
    facesMessage.setDetail(message);
    context.addMessage("isbn", facesMessage);
}

当我点击“添加”按钮时,应该将该书添加到数据库中。

如果未填写ISBNfield,namefield或titlefield,我会收到相应的错误消息。但是当填写我的字段并且ISBN验证失败时,他会显示错误消息,但他仍然会将该书(带有错误的ISBN编号)添加到数据库中。

我想到的解决方案:如果我的消息标签不为空,则不应将该书添加到数据库中。但我怎么检查呢?

或者我的问题有更好的解决方案吗?

1 个答案:

答案 0 :(得分:3)

验证错误消息处理错误。您需要抛出 ValidatorException FacesMessage,而不是手动添加FacesMessage

所以不是所有那些

addMessageToContext(context, "- ISBN needs to have 17 characters");

你需要做

throw new ValidatorException(new FacesMessage("- ISBN needs to have 17 characters"));

对于JSF签名,输入无效,因此不会调用action方法。您也不需要指定客户端ID,它最终会在与触发此验证器的输入组件关联的<h:message>中结束。