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

  • Home
  • SEARCH
  • 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 7163887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:58:40+00:00 2026-05-28T13:58:40+00:00

So I have a String I need to check for a valid format for

  • 0

So I have a String I need to check for a valid format for which I used a pattern, and whether the entered value is valid according to some business rules for which I used a custom validator. Now the customer would prefer it if the error message associated with the latter is only shown if the former didn’t occur. But as far as I know there is no way to make this distinction.
Or can anyone think of a way to do this?

  • 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-28T13:58:40+00:00Added an answer on May 28, 2026 at 1:58 pm

    The trick is: have two different constraints (annotations),

    • one for the pattern, that accept null,
    • and a second for not null.

    By default the javax.validation.constraints.Pattern “validator” accept null.

    Accepts String. null elements are considered valid.
    (javax.validation.constraints.Pattern javadoc)

    So what you need to do in the end is this:

    @NotNull(message="{validation.notNull}")
    @Pattern(pattern="123" message="{validation.notPattern}")
    String myString;
    

    Added:

    (Comment)

    Not really the desired behaviour since I want to reuse the bean object but the conditions aren’t the same for each page. – Jack Nickels 5 mins ago

    In this case you can use the so called groups (See JSR303 Bean Validation Spec, Chapter 4.1.2 groups — there is also an example). Use the default Group for @Pattern and an other group for @NotNull. Now you can enable or disable the Validation rules according the groups you specify for validation.

    validator.validate(myObject, Default.class);
    validator.validate(myObject, MyGroup.class);
    

    There is one problem left: in Spring 3.0 you can not Specify the Group you want to use for the automatic validation process. But in 3.1 you can, according to that Feature Request SPR-6373 (I have not tryed it, but I hope it works)

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

Sidebar

Related Questions

I have a string which I need to verify if it's a Country code.
I have a string and I need to find out whether it is a
Say, I have a string that I need to verify the correct format of;
I need to have a string, based on an integer, which should always have
I have a string: 31-02-2010 and want to check whether or not it is
I have a string with keywords and I need to check if this string
I need a function to check a date with boolean. I have a String
Hi I need to use php's pregmatch to check a string is valid. In
I have a string buffer of about 2000 characters and need to check the
I have a string I need to feed to a com object that has

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.