Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5944203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:30:15+00:00 2026-05-22T16:30:15+00:00

I have a small webapplication for a library, with a customized ISBN validator. My

  • 0

I have a small webapplication for a library, with a customized ISBN validator. My .xhtml page for adding a book looks like this:

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

The isbnValidator of the first input field is this class:

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);
}

When I click the ‘add’ button, the book should be added to the database.

When the ISBNfield, namefield or titlefield aren’t filled in, I get the corresponding error message. But when my fields are filled in, and the ISBN validation fails, he shows the error message, but he still adds the book (with a wrong ISBN-number) to the database.

A solution which I thought about: If my messages tags is not empty, he shouldn’t add the book to the database. But how can I check that?

Or is there a better solution to my problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:30:16+00:00Added an answer on May 22, 2026 at 4:30 pm

    The validation error message handling is wrong. You need to throw a ValidatorException with a FacesMessage instead of adding the FacesMessage manually.

    So instead of all those

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

    you need to do

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

    This is for JSF sign that the input is invalid and hence the action method won’t be invoked. You also don’t need to specify the client ID, it will just end up in the <h:message> associated with the input component where this validator is fired on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have small page which has label, DropDownList and a submit button. <div> <asp:label
I have a small webshop-like web application, and i have big plans :-) I
I have a small javascript library written by myself. I want to reference it
I have a large web application. One very small piece of this web application
I have a webapplication which id like to host online. The user logon consists
I have a simple idea for a small web application that I'd like to
I have this webapplication tool which queries over data and shows it in a
I am not sure if this is possible but I have a small web
I have a small JSF web application on WAS7.0 with just one page but
I'm working with this small web application (1 page) built by someone else that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.