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 5971813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:34:11+00:00 2026-05-22T20:34:11+00:00

I’m using JSF2 on GlassFish 3. I have a form that accepts and optional

  • 0

I’m using JSF2 on GlassFish 3.

I have a form that accepts and optional phone number. I have this custom phone number validator (below), and I have the field set to required=”false” because the phone number is optional in the form.

The problem is, the value in the field is always getting validated. Shouldn’t the validation be skipped when there is no value specified?

There must be something I’m doing wrong. Any help is appreciated, thanks!

<h:outputText value="#{msg.profile_otherPhone1Label}#{msg.colon}" />
  <h:panelGroup>
    <p:inputText label="#{msg.profile_otherPhone1Label}" id="otherPhone1" value="#{profileHandler.profileBean.otherPhone1}" required="false">
      <f:validator validatorId="phoneValidator" />
    </p:inputText>
  <p:spacer width="12"/>
  <h:outputText value="#{msg.profile_phoneExample}" />
</h:panelGroup>

#

public class PhoneValidator implements Validator {

    @Override
    public void validate(FacesContext facesContext, UIComponent uIComponent,
            Object object) throws ValidatorException {

        String phone = (String) object;

        // count the digits; must have at least 9 digits to be a valid phone number
        // note: we're not enforcing the format because there can be a lot of variation
        int iDigitCount = 0;
        for (char c : phone.toCharArray()) {
            if (Character.isDigit(c)) {
                iDigitCount++;
            }
        }

        if (iDigitCount < 9) {
            FacesMessage message = new FacesMessage();
            message.setSummary(Messages.getMessage("error_phoneNumberNotValid"));
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }
    }

}
  • 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-22T20:34:12+00:00Added an answer on May 22, 2026 at 8:34 pm

    From JSF 2.0 on, JSF will by default validate empty fields as well in order to play well with the new Java EE 6 provided JSR 303 Bean Validation API which offers among others @NotNull and on.

    There are basically two ways to go around this:

    1. Tell JSF to not validate empty fields by the following entry in web.xml.

      <context-param>
          <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
          <param-value>false</param-value>
      </context-param>
      

      The disadvantage is obvious: you can’t utilize JSR 303 Bean Validation at its full powers anymore.


    2. Do a nullcheck yourself in the Validator.

      if (object == null) return;
      

      If you don’t have a javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL context param which is set to true, then you’d like to cast to String and check isEmpty() as well.

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am using Paperclip to handle profile photo uploads in my app. They upload
I am writing an app with both english and french support. The app requests
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.