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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:31:15+00:00 2026-06-14T05:31:15+00:00

I created my first custom validation annotation with validator class as inner class (which

  • 0

I created my first custom validation annotation with validator class as inner class (which i find quite well-arranged).
It looks like this:

@Target( { ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {OneOfGroupNotNull.Validator.class})
@Documented
public @interface OneOfGroupNotNull {

    // custom annotation properties
    String[] fields();

    // required by JSR-303
    String message() default "One of group must be not null. {fields}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};

    public class Validator implements ConstraintValidator<OneOfGroupNotNull, Object> {

        private String[] fields;

        @Override
        public boolean isValid(Object bean, ConstraintValidatorContext cvc) {

            int countNotNull = 0;

            for (String field : fields) {
                try {
                    String property = BeanUtils.getProperty(bean, field);
                    if (property != null) {
                        countNotNull++;
                    }
                } catch (Exception ex) {
                    throw new RuntimeException("Validation for field " + field + " of type " + bean.getClass()+ " raised exception.", ex);
                }
            }

            return countNotNull == 1;
        }

        @Override
        public void initialize(OneOfGroupNotNull a) {
            fields = a.fields();
        }
    }
}

A bean class that is annotated with this validator may look like this:

@OneOfGroupNotNull(fields = {"a", "b", "c"})
public interface MyBean {
    String getA();
    Rarity getB();
    Boolean getC();
}

The problem is that I cannot find a way to format the string array “fields”. It just takes the to string method which results in something like:
One of group must be not null. [Ljava.lang.String;@157d954

  • 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-06-14T05:31:17+00:00Added an answer on June 14, 2026 at 5:31 am

    If you changed type of fields from String[] to String then message with field names will be shown correctly. To get field names in constraint just split() it by comma.

    Another option is to generating custom message inside constraint, like this:

    cvc.disableDefaultConstraintViolation();
    cvc.buildConstraintViolationWithTemplate("error message")
        .addNode("field name with error")
        .addConstraintViolation();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a few RegEx for my jQuery validator custom methods. The first which
I have some problems with my first Open Graph custom action: I've created a
I had been created text box custom validation control and I'm using this in
I have a fiddle in which i am using knockout.js. I created a custom
I have created a custom validator deriving from ValidationAttribute. My undertsandng is that it
At first my programm looks like this: But when i am moving the picture
I've created my first custom Umbraco 5.1 solution. At this point I have a
When I first created my app, I stored all my runtime custom objects and
I recently created my first custom control. I attempted to add it in a
I recently created my first custom control. I attempted to add it in a

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.