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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:47:17+00:00 2026-05-26T20:47:17+00:00

Play 1.0 comes with a full featured validation framework base on http://oval.sourceforge.net/ . With

  • 0

Play 1.0 comes with a full featured validation framework base on http://oval.sourceforge.net/.

With the release of 2.0, my custom validators do not work anymore.

How does one create custom validator using Play Framework 2.0 ?

  • 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-26T20:47:17+00:00Added an answer on May 26, 2026 at 8:47 pm

    In Play 2.0, the validation framework extends beyond the actual validation of the data as it reaches to:

    • Annotations – to easily declare validation contraints using the ‘@’ sign
    • Validators – which actually implements to logic behind the validation
    • Messages – to display parametrized error messages (i18 compliant)
    • Finally, HTML helpers – that glue all the previous together

    The HTML Helpers are something new to Play 2.0. In 1.x, Play was already pretty good at enforcing a well defined validation framework. It was powerful and easy to use. Yet we still had to wire the HTML form and the validation framework together. This could be a little confusing to the beginner.

    With Play 2.0, this is now done automatically.

    But let’s focus on the answer and provide some guidance: We will create an AllUpperCase validator, that generates an error either when:

    • the input is not a String
    • the input is empty
    • one of the characters is lower-case.

    The validator

    package myvalidators;
    
    import javax.validation.*;
    
    public class AllUpperCaseValidator 
            extends play.data.validation.Constraints.Validator<Object> 
            implements ConstraintValidator<AllUpperCase, Object> {
    
        /* Default error message */
        final static public String message = "error.alluppercase";
    
        /**
         * Validator init
         * Can be used to initialize the validation based on parameters
         * passed to the annotation.
         */
        public void initialize(AllUpperCase constraintAnnotation) {}
    
        /**
         * The validation itself
         */
        public boolean isValid(Object object) {
            if(object == null)
                return false;
    
            if(!(object instanceof String))
                return false;
    
            String s = object.toString();  
            for(char c : s.toCharArray()) {
                if(Character.isLetter(c) && Character.isLowerCase(c))
                    return false;
            }
    
            return true;
        }
    
        /**
         * Constructs a validator instance.
         */
        public static play.data.validation.Constraints.Validator<Object> alluppercase() {
            return new AllUpperCaseValidator();
        }
    }
    

    The first thing you may notice is the import: Play 2.0 indeed complies with JSR 303 – Bean Validation Framework. In this context, the validator needs to implement ConstraintValidator. Which in our case translates into the annotation as class AllUpperCase (which we will introduce in a minute) and T as a generic Object.

    The validator is straighforward:
    We defined the method public boolean isValid(Object object) that returns a boolean, if true the validation passed. There is also an message id and a method that instanciates the validator.

    The Annotation

    The class below defines an annotation named @AllUpperCase which takes no parameters but enforces the validation defined previously. Providing details related to the annotation framework is outside the scope of this post.

    package myvalidators;
    
    import java.lang.annotation.*;
    import static java.lang.annotation.ElementType.*;
    import static java.lang.annotation.RetentionPolicy.*;
    
    import javax.validation.*;
    
    @Target({FIELD})
    @Retention(RUNTIME)
    @Constraint(validatedBy = AllUpperCaseValidator.class)
    @play.data.Form.Display(name="constraint.alluppercase")
    public @interface AllUpperCase {
        String message() default AllUpperCaseValidator.message;
        Class<?>[] groups() default {};
        Class<? extends Payload>[] payload() default {};
    }
    

    Note how the anotation glues to the other pieces of the puzzle.

    • @Constraint, a JSR 303 annotation, links to the validator
    • @play.data.Form.Display, links the annotation to the play html helpers. Note that the name is important: we are defining a constraint named alluppercase. Play uses this information to call the method public static play.data.validation.Constraints.Validator<Object> alluppercase() on the Validator.
    • Finally note that the default message is set within the anotation interface.

    Usage

    We now have our custom validator and annotation

    import myvalidators.*;
    public static class MyData {
        @AllUpperCase
        public String name;
    }
    

    Describing the usage is outside the scope of this post, please find a working sample at this URL

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

Sidebar

Related Questions

I use the in-memory database that comes with Play Framework, when I have db=mem
I'm currently using jQuery.validate as a plugin for validation... my problem comes into play
Information theory comes into play where ever encoding & decoding is present. For example:
Anyone clear on how big a role Javascript will play when HTML 5 comes
Hi I'm new to OOP with PHP. I'm wondering how OO comes into play
Play frameworks comes with an old Hibernate version (3.6.1). How can I update Hibernate
I'm a bit confused about how the OneToOneField works when deletion comes into play.
I am building a Java web app, using the Play! Framework . I'm hosting
I started trying to play with Mono, mostly for fun at the moment. I
I've started to play around with PowerShell and am trying to get it to

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.